1136 A Delayed Palindrome

#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
string add(string a,string b){
	int carry=0;
	string sum;
	for(int i=a.length()-1;i>=0;i--){
		int temp=a[i]-'0'+b[i]-'0'+carry;
		if(temp>=10){
			carry=1;
			sum+=(temp%10+'0');
		}else{
			carry=0;
			sum+=(temp+'0');
		}
	}
	if(carry==1){
		sum+=1+'0';
	}
	reverse(sum.begin(),sum.end());
	return sum;
}
int main(){
	string s,t,f;
	cin>>s;
	int i;
	for(i=0;i<10;i++){
		t=s;
		reverse(s.begin(),s.end());
		if(s==t){
			cout<<s<<" is a palindromic number."<<endl;
			break;
		}
		f=add(s,t);
		cout<<t<<" + "<<s<<" = "<<f<<endl;
		s=f;
		string temp=f;
		reverse(temp.begin(),temp.end());
		if(f==temp){
			cout<<f<<" is a palindromic number."<<endl;
			break;
		}
	}
	if(i==10) cout<<"Not found in 10 iterations."<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/csg3140100993/article/details/81388563
今日推荐