注意一下6174的情况
简单题
#include <cstdio>
#include <algorithm>
using namespace std;
int cmp(int a,int b)
{
return a>b;
}
int decrease(int n)
{
int num[5];
int ans=0;
for(int i = 0;i<4;i++)
{
num[i]=n%10;
n/=10;
}
sort(num,num+4,cmp);
ans = num[0]*1000 + num[1] * 100 +num[2]*10 +num[3];
return ans;
}
int increase(int n)
{
int num[5];
int ans=0;
for(int i = 0;i<4;i++)
{
num[i]=n%10;
n/=10;
}
sort(num,num+4);
ans = num[0]*1000 + num[1] * 100 +num[2]*10 +num[3];
return ans;
}
int main()
{
int n,temp=0,fans = 0;
scanf("%d",&n);
while(1)
{
int num1 = decrease(n);
int num2 = increase(n);
n = num1 - num2;
printf("%04d - %04d = %04d\n",num1,num2,n);
if(n==0 || n==6174)break;
}
return 0;
}