代码:
#include<iostream> #include<cstring> using namespace std; int main(void) { char snumber[1001],snumber1[1001],snumber2[1001],answer[1001][1001]; int len1,flag,a,k,i,j; while(cin>>snumber) { strcpy(snumber1,snumber); strrev(snumber1);//将数字倒置 j=1; strcpy(answer[0],snumber); //大数相加,再加上点处理 while(strcmp(snumber,snumber1)) { k=1; len1=strlen(snumber); a=snumber[0]-'0'; memset(snumber2,0,sizeof(snumber2)); for(i=0;i<len1;i++) snumber2[k++]=snumber[i]+snumber1[i]-'0'; for(i=len1;i>=2;i--) { if(snumber2[i]>'9') { snumber2[i]-=10; snumber2[i-1]++; } } flag=0; if(snumber2[1]>'9') { snumber2[1]-=10; snumber2[0]='1'; flag=1; } memset(snumber,0,sizeof(snumber)); if(!flag) for(i=0;i<len1;i++) { snumber[i]=snumber2[i+1]; } else for(i=0;i<=len1;i++) { snumber[i]=snumber2[i]; } strcpy(answer[j++],snumber); strcpy(snumber1,snumber); strrev(snumber1); } cout<<j-1<<endl; for(i=0;i<j-1;i++) cout<<answer[i]<<"--->"; cout<<answer[j-1]; cout<<endl; } return 0; } |