
用了柳婼大佬博客的思路,但实现有不同
没有用string所以要考虑字符串末尾的‘\0’
用的stl中的reverse逆置字符串
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
int main(){
char A[1009]={'\0'},B[1009]={'\0'},C[1009]={'\0'};;
scanf("%s",A);
int cut=0;
for(int i=0;i<10;i++){
strcpy(B,A);
reverse(B,B+strlen(B));
if(strcmp(A,B)==0){
printf("%s is a palindromic number.",A);
return 0;
} //B逆置
int flag=0;
for(int j=0;j<strlen(B);j++){ //C=A+B;
C[j]=B[j]+A[j]-'0';
if(flag==1){
C[j]++;flag=0;
}
if(C[j]>'9'){
C[j]-=10;
flag=1;
}
}
if(flag==1)
C[strlen(C)]='0'+1;
reverse(C,C+strlen(C));
printf("%s + %s = %s\n",A,B,C);
cut++;
strcpy(A,C);
}
if(cut==10)printf("Not found in 10 iterations.");
return 0;
}