#include <cstdio>
#include <algorithm>
using namespace std;
void toarr(int k, int arr[]){
for(int i=3; i>=0; i--){
arr[i] = k % 10;
k /= 10;
}
}
int toint(int arr[]){
int sum = arr[0];
for(int i=1; i<4; i++){
sum = sum*10 + arr[i];
}
return sum;
}
bool cmp(int a, int b){
return a > b;
}
int main(){
int n;
scanf("%d", &n);
int arr[4];
toarr(n, arr);
int i;
for(i=1; i<4; i++){
if(arr[i] != arr[i-1]){
break;
}
}
if(i == 4){
printf("%04d - %04d = 0000", n, n);
return 0;
}
int res = n;
int num1, num2;
if(res == 6174){
printf("7641 - 1467 = 6174");
return 0;
}
while(res != 6174){
sort(arr, arr+4);
num1 = toint(arr);
sort(arr, arr+4, cmp);
num2 = toint(arr);
res = num2 - num1;
printf("%04d - %04d = %04d\n", num2, num1, res);
toarr(res,arr);
}
return 0;
}
PAT B1019 数字黑洞 (20分)
最新推荐文章于 2024-11-09 20:38:27 发布