1069 The Black Hole of Numbers(20 分)

#include
#include
#include
using namespace std;
int n, cnt;
int getH(int a[])
{
    sort(a, a + 4, greater());
    int sum = 0;
    for (int i = 0; i < 4; i++)sum = sum * 10 + a[i];
    return sum;
}
int getL(int a[])
{
    sort(a, a + 4);
    int sum = 0;
    for (int i = 0; i < 4; i++)sum = sum * 10 + a[i];
    return sum;
}
int main()
{
    scanf("%d", &n);
    do {
        int num[1000];
        cnt = 0;
        for (int i = 0; i < 4; i++)num[i] = n % 10, n /= 10;
        int high = getH(num);
        int low = getL(num);
        n = high - low;
        printf("%04d - %04d = %04d\n", high, low, n);
    } while (n != 6174 && n != 0);
    return 0;
}

你可能感兴趣的:(1069 The Black Hole of Numbers(20 分))