1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <cstdio> #include <algorithm> using namespace std;
bool cmp(int a, int b){ return a > b; } void to_array(int n, int num[]){ for (int i = 0; i < 4; i++){ num[i] = n % 10; n /= 10; } } int to_num(int num[]){ int sum = 0; for (int i = 0; i < 4; i++) sum = sum * 10 + num[i]; return sum; }
int main(){ int n, max, min, difference = 1; int num[5]; scanf("%d", &n); while (difference != 0 && difference != 6174){ to_array(n, num); sort(num, num + 4); min = to_num(num); sort(num, num + 4,cmp); max = to_num(num); n = difference = max - min; printf("%04d - %04d = %04d\n", max, min, difference); }
}
|