PAT1024. Palindromic Number (25)

输入即为回文的情况要考虑

 1 #include <iostream>
 2 #include <algorithm> //reverse
 3 using namespace std;
 4 string n;
 5 int k;
 6 string strAdd(string a){
 7     string b;
 8     int num,cc=0;
 9     int len=a.size();
10     for(int i=0;i<len;i++){
11         num=a[i]+a[len-i-1]-2*'0'+cc;
12         cc=num/10;
13         b.push_back(num%10+'0');
14     }
15     if(cc!=0) b.push_back('1');
16     reverse(b.begin(),b.end());
17     return b;
18 }
19 bool isPali(string &a){
20     int len=a.size();
21     for(int i=0;i<len/2;i++){
22         if(a[i]==a[len-1-i]){continue;}
23         else{return false;}
24     }
25     return true;
26 }
27 int main()
28 {
29     cin>>n>>k;
30     int c=k;
31     string result=n;
32     if(isPali(result)) {cout<<result<<endl<<"0";return 0;}
33     while(c--){
34         result=strAdd(result);
35         if(isPali(result))break;
36     }
37     if(c<=0){
38         cout<<result<<endl;
39         cout<<k;
40     }else{
41         cout<<result<<endl;
42         cout<<(k-c);
43     }
44     return 0;
45 }

 

posted @ 2016-03-22 11:59  Yellowman  阅读(131)  评论(0编辑  收藏  举报
TVRBMExqRXlPQzR5TXpjdU1UVTEK\n