酷代码 AI
菜单

给出n个字符串,要求将n个字符串按顺序拼接,拼接之后判断是不是回文串,是就输出yes,不是就输出no。 输入: 第1行,一个正整数n,表示字符串的个数 第2行到第n+1行,n个字符串 输出: 是回文串输出yes,不是输出no。c++

```cpp #include <iostream> #include <string> // 检查字符串是否为回文串的函数 bool isPalindrome(const std::string& s) { int left = 0; int right = s.length() - 1; while (left < right) { if (s[left] != s[right]) { return false; } left++; right--; } return true; } int main() { int n; std::cin >> n; // 读取换行符,防止影响后续字符串读取 std::cin.ignore(); std::string combined; for (int i = 0; i < n; i++) { std::string str; std::getline(std::cin, str); combined += str; } if (isPalindrome(combined)) { std::cout << "yes" << std::endl; } else { std::cout << "no" << std::endl; } return 0; } ``` 这段C++代码实现的功能是将输入的多个字符串按顺序拼接起来,然后判断拼接后的字符串是否是回文串。具体步骤为,先读取输入的字符串个数`n`,再逐个读取字符串并拼接成一个长字符串`combined`,最后调用`isPalindrome`函数判断`combined`是否为回文串,根据结果输出`yes`或`no`。 [2026-01-31 21:12:14 | AI问答 | 346点数解答]

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