首页 > 学院 > 开发设计 > 正文

1024. Palindromic Number (25)

2019-11-11 02:07:10
字体:
来源:转载
供稿:网友

题目不难,开始错误的原因是用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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表