好强大的两个函数。具体详细的使用方法去百度吧,各种信息,只是在此纪念下。。
#include <string>
#include <algorithm>
using namespace std;
int get_next(int x)
{
int a, b, n;
char s[10];
sprintf(s,"%d",x);
n = strlen(s);
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
if(s[i]>s[j])
{
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
sscanf(s,"%d",&b);
for(int i=0; i<n/2; i++)
{
char t = s[i];
s[i] = s[n-1-i];
s[n-1-i] = t;
}
sscanf(s,"%d",&a);
return a-b;
}
int num[2000],step;
int main()
{
scanf("%d",&num[0]);
printf("%d", num[0]);
step =1;
while(1)
{
num[step] = get_next(num[step-1]);
printf("-> %d",num[step]);
int found = 0;
for(int i=0; i<step; i++)
if(num[i]==num[step])
{
found = 1;
break;
}
if(found) break;
step++;
}
system("pause");
return 0;
}