注意点:如果输入的数是 回文数,那么无需计算,直接输出即可。
这题和 B1019数字黑洞 类似,用string处理 即可!
ps:第一次做时,写了68行,第二次做时,写了38行。。。
#include<iostream> #include<algorithm> using namespace std; bool isPalindromicNumber(const string& str) {//判断是否是回文数 int n = str.size(); for(int i = 0; i < n/2; ++i) if(str[i] != str[n-1-i]) return false; return true; } int main() { string str; cin>>str; int i; for(i = 0; i < 10; ++i) { if(isPalindromicNumber(str)) { printf("%s is a palindromic number.",str.c_str()); break; } string temp = str; reverse(temp.begin(),temp.end()); printf("%s + %s = ",str.c_str(),temp.c_str()); string result; int carry = 0; for(int i = str.size()-1; i >= 0; --i) { result += (str[i]-‘0‘+temp[i]-‘0‘+carry)%10 +‘0‘; carry = (str[i]-‘0‘+temp[i]-‘0‘+carry)/10; } if(carry != 0) result += carry +‘0‘; reverse(result.begin(),result.end()); printf("%s\n",result.c_str()); str = result; } if(i == 10) printf("Not found in 10 iterations."); return 0; }
原文:https://www.cnblogs.com/keep23456/p/12362251.html