酷代码 AI
菜单

给出 n 个数字,要求将数字转换成对应的字符,并将其中的英文字母以小写形式输出。c++

```cpp #include <iostream> #include <vector> #include <string> #include <cctype> std::string convertNumbersToChars(const std::vector<int>& numbers) { std::string result; for (int num : numbers) { if (num >= 0 && num <= 127) { // ASCII范围检查 char ch = static_cast<char>(num); if (std::isalpha(ch)) { result += std::tolower(ch); // 字母转小写 } else { result += ch; // 非字母字符直接添加 } } } return result; } int main() { // 示例输入 std::vector<int> numbers = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33}; std::string output = convertNumbersToChars(numbers); std::cout << "转换结果: " << output << std::endl; return 0; } ``` 代码说明: - `convertNumbersToChars` 函数接收整数向量,返回转换后的字符串 - 遍历每个数字,检查是否在有效ASCII范围内(0-127) - 使用 `std::isalpha` 判断是否为字母字符 - 字母字符通过 `std::tolower` 转换为小写 - 非字母字符直接保留原形式 示例输出: ``` 转换结果: hello world! ``` 输入说明: - 输入应为ASCII码值(0-127) - 支持数字、标点、字母等可打印字符 - 字母自动转换为小写形式 [2025-11-23 14:58:13 | AI问答 | 362点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多