1079 延迟的回文数 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805261754023936
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstdlib> 8 using namespace std; 9 bool ispalindrome(string a); 10 string add(string b,string c); 11 int main() 12 { 13 string a,b,c; 14 bool flag=false; 15 cin>>a; 16 if(ispalindrome(a)) {cout<<a<<" is a palindromic number.\n"; return 0;} 17 int cnt=10; 18 while(cnt>0) 19 { 20 b=a; 21 reverse(b.begin(),b.end()); 22 c=add(a,b); 23 cout<<a<<" + "<<b<<" = "<<c<<endl; 24 if(ispalindrome(c)) {cout<<c<<" is a palindromic number.\n"; flag=true; break;} 25 a=c; 26 cnt--; 27 } 28 if(!flag) cout<<"Not found in 10 iterations.\n"; 29 return 0; 30 } 31 bool ispalindrome(string a) 32 { 33 int len=a.size(); 34 for(int i=0;i<len/2;i++) 35 { 36 if(a[i]!=a[len-1-i]) return 0; 37 } 38 return 1; 39 } 40 string add(string a,string b) 41 { 42 string d; 43 int adv=0,bac=0; 44 int len=a.size(); 45 for(int i=len-1;i>=0;i--) 46 { 47 bac=(a[i]-‘0‘)+(b[i]-‘0‘); 48 d+=(bac+adv)%10+‘0‘; 49 adv=(bac+adv)/10; 50 } 51 if(adv) d+=(adv)+‘0‘; 52 reverse(d.begin(),d.end()); 53 return d; 54 }
原文地址:https://www.cnblogs.com/jianqiao123/p/12203316.html
时间: 2024-10-04 08:21:09