PAT(Basic Level) Practice : 1079 延迟的回文数 (20分)
1079 延迟的回文数 (20分)
测试点234
测试点234是输入就是回文数的情况,特殊判断即可。
代码
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
//scanf printf防止超时
#include <algorithm>
//vector的sort
#include <sstream>
//转换
using namespace std;
#include<iomanip>
//精度
#include<cmath>
//round四舍五入取整
bool judge(string str)
{
int mid=str.length()/2;
bool flag=true;
for(int i=0;i<mid;i++)
{
if(str[i]!=str[str.length()-1-i])
flag=false;
}
return flag;
}
int main()
{
string str;
cin>>str;
if(judge(str))
{
cout<<str<<" is a palindromic number."<<endl;
}else{
for(int i=0;i<10;i++)
{
cout<<str<<" + ";
for(int j=str.length()-1;j>=0;j--)
{
cout<<str[j];
}
cout<<" = ";
int flag=0;
string res="";
for(int j=0;j<str.size();j++)
{
int sum=str[j]-'0'+str[str.size()-1-j]-'0'+flag;
if(sum>=10)
{
sum-=10;
flag=1;
}else
{
flag=0;
}
char temp=sum+'0';
res=temp+res;
}
if(flag==1)
res="1"+res;
cout<<res<<endl;
if(judge(res))
{
cout<<res<<" is a palindromic number."<<endl;
str=res;
break;
}
str=res;
}}
if(!judge(str))
{
cout<<"Not found in 10 iterations."<<endl;
}
return 0;
}