6174问题
样例输入:1234
样例输出:1234-> 3087-> 8352-> 6174-> 6174
(4321-1234=3087,8730-378=8352,8532-2358=6174)
↑ ↑
将3087从大到小排序 再反转
1 #include<cstdio> 2 #include<string.h> 3 4 int get_next(int x) 5 { 6 int a,b,n; 7 char t; 8 int i,j; 9 char s[10]; 10 sprintf(s,"%d",x); 11 n=strlen(s); 12 for(i=1;i<n;i++) 13 { 14 for(j=n-1;j>=i;j--) 15 { 16 if(s[j]<s[j-1]) 17 { 18 t=s[j]; 19 s[j]=s[j-1]; 20 s[j-1]=t; 21 } 22 } 23 } 24 sscanf(s,"%d",&b); 25 for(i=0;i<n/2;i++) 26 { 27 t=s[i]; 28 s[i]=s[n-1-i]; 29 s[n-1-i]=t; 30 } 31 sscanf(s,"%d",&a); 32 return a-b; 33 } 34 35 int main() 36 { 37 int num[2000],count; 38 int i; 39 scanf("%d",&num[0]); 40 printf("%d",num[0]); 41 count=1; 42 for(;;) 43 { 44 num[count]=get_next(num[count-1]); 45 printf("-> %d",num[count]); 46 int found=0; 47 if(num[count]==num[count-1]) 48 found=1; 49 if(found) 50 break; 51 count++; 52 } 53 printf(" "); 54 return 0; 55 }