6174问题
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num[2000], count;
int Comp(const void* _a, const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int get_next(int x);
int main()
{
scanf("%d",&num[0]);
printf("%d",num[0]);
count=1;
while(true){
num[count]=get_next(num[count-1]);
printf(" -> %d",num[count]);
int found=0;
for(int i=0;i<count;i++){
if(num[i]==num[count]){
found=1;
break;
}
}
if(found){
break;
}
count++;
}
printf("%\n");
return 0;
}
int get_next(int x)
{
int a, b, n;
char s[10];
sprintf(s,"%d",x);
n=strlen(s);
qsort(s,n,sizeof(char),Comp);
sscanf(s,"%d",&b);
//反转字符串.
for(int i=0;i<n/2;i++){
int t=s[i];
s[i]=s[n-1-i];
s[n-1-i]=t;
}
sscanf(s,"%d",&a);
return a-b;
}