For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.
IDEA
1.用到了string的翻转函数reverse()
2.int, long long都会溢出,考查string类型大数加法
3.新学到一个string的插入函数insert(str.begin, c)//每次都在str的开头插入一个字符
CODE
#include<iostream> #include<algorithm> #include<sstream> #include<cstring> #include<cmath> using namespace std; int isPalindromic(string str){ for(int i=0,j=str.length()-1;i<str.length()/2;i++,j--){ if(str[i]!=str[j]){ return 0; } } return 1; } string strPlus(string a,string b){ string result=""; int len_a=a.length(),len_b=b.length(); int sum,flag=0;//flag=1表示进位 for(int i=len_a-1,j=len_b-1;i>=0||j>=0;i--,j--){ if(i<0){ sum=b[j]-'0'; }else if(j<0){ sum=a[i]-'0'; }else{ sum=a[i]-'0'+b[j]-'0'; } sum+=flag; result.insert(result.begin(),sum%10+'0'); //在begin出插入字符,即每次都在头部插入字符 flag=sum/10; } if(flag){ result.insert(result.begin(),flag+'0'); } return result; } int main(){ string n; int k; cin>>n>>k; int count=0; string tmp=n; while(count<=k){ if(isPalindromic(tmp)){ cout<<tmp<<endl<<count; break; } if(count==k){ cout<<tmp<<endl<<k; break; } string tmp1=tmp; reverse(tmp1.begin(),tmp1.end()); tmp=strPlus(tmp,tmp1); count++; } return 0; }