題目不難,開(kāi)始錯(cuò)誤的原因是用str=tostring(stoll(str)+stoll(str2))來(lái)處理結(jié)果,導(dǎo)致溢出(str存儲(chǔ)的數(shù)值可能會(huì)遠(yuǎn)超過(guò)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;}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注