#include<stdio.h>
#include<math.h>
int cmp1( const void *a, const void *b){
return *(int *)a - *(int *)b;//升序
}
int cmp2(const void *a, const void *b){
return *(int *)b - *(int *)a;//降序
}
int max(int x)
{
int num[5];
int i = 3;
while(x)
{
num[i] = x % 10;
x /= 10;
i--;
}
qsort(num,4,sizeof(num[0]),cmp2);
return num[0]*1000+num[1]*100+num[2]*10+num[3];
}
int min(int x)
{
int num[5];
int i = 3;
while(x)
{
num[i] = x % 10;
x /= 10;
i--;
}
qsort(num,4,sizeof(num[0]),cmp1);
return num[0]*1000+num[1]*100+num[2]*10+num[3];
}
int main()
{
int n;
scanf("%d",&n);
if(n/1000 == n/100%10 == n/10%10 == n % 10) printf("%d - %d = 0000",n,n);
else {
int x,y,z;
x = max(n);
y = min(n);
z = x - y;
while(z != 6174)
{
printf("%04d - %04d = %04d\n",x,y,z);
x = max(z);
y = min(z);
z = x - y;
}
printf("%04d - %04d = %04d",x,y,z);
}
return 0;
}
AcWing 1555. 数字黑洞
于 2023-03-22 17:00:30 首次发布