题目不难,开始错误的原因是用str=tostring(stoll(str)+stoll(str2))来处理结果,导致溢出(str存储的数值可能会远超过10^10)
#include<iostream>#include<string>using namespace std;int main(){ string str; cin >> str; int K; cin >> K; int t = 0; while(1) { string str2; for (auto it = str.rbegin();it != str.rend();it++) str2.push_back(*it); if (str == str2 || t==K) break; else { int temp = 0; for (int i = str.size()-1;i >= 0;i--) if (str[i]+str2[i]-'0'-'0'+temp > 9) { str[i] = str[i]-'0' + str2[i] + temp - 10; temp = 1; } else { str[i] = str[i]-'0' + str2[ i] + temp; temp = 0; } if (temp == 1) str.insert(str.begin(), '1'); t++; } } if (t != K + 1) cout << str << endl << t << endl; else cout << str << endl << K << endl;}新闻热点
疑难解答