Run ID:115518
提交时间:2025-03-31 15:16:00
#include <iostream> #include <string> #include <algorithm> // 用于 std::reverse using namespace std; // 函数:统计字符串中数字的个数 int countDigits(const string& str) { int count = 0; for (char c : str) { if (isdigit(c)) { count++; } } return count; } int main() { // 输入两个字符串 string str1, str2; getline(cin, str1); getline(cin, str2); // 统计每个字符串中的数字个数 int count1 = countDigits(str1); int count2 = countDigits(str2); // 确定数字最多的字符串和另一个字符串 string maxStr, otherStr; if (count1 >= count2) { maxStr = str1; otherStr = str2; } else { maxStr = str2; otherStr = str1; } // 逆序输出另一个字符串 reverse(otherStr.begin(), otherStr.end()); // 如果数字最多的字符串中有足够的字符,拼接第 n 个字符 if (count1 > 0 && count1 <= maxStr.size()) { otherStr += maxStr[count1 - 1]; // 数字个数为 count1,第 n 个字符索引为 count1 - 1 } // 输出结果 cout << otherStr << endl; return 0; }