PAT 1069. The Black Hole of Numbers

我觉得做人最重要的是把事情讲清楚,尤其是规则,如果法律条例不清楚,就可以寻租了

#include <cstdio>

#include <cstdlib>

#include <vector>

#include <algorithm>



using namespace std;



int ensure(int n) {

    if (n < 10) {

       n *= 1000;

    } else if (n < 100) {

       n *= 100;       

    } else if (n < 1000) {

       n *= 10;       

    }

    return n;

}



void extend_print(int n) {

     if (n < 10) {

        printf("000%d", n);      

     } else if (n < 100) {

        printf("00%d", n);       

     } else if (n < 1000) {

        printf("0%d", n);

     } else {

        printf("%d", n);

     }

}



void print_equal(int a, int b, int c) {

     extend_print(a);

     printf(" - ");

     extend_print(b);

     printf(" = ");

     extend_print(c);

     printf("\n");    

}



bool mycmp(int a, int b) {

     if (a >= b) return true;

     return false;     

}



int reverse(int n) {

    int v = 0;

    while (n) {

          v = v * 10 + n % 10;

          n/= 10;

    }

    return v; 

}



int sort_digits(int n) {

    int ds[5] = {0};

    int len = 0;

    while (n) {

          ds[len++] = n % 10;

          n/=10;

    }

    if (len == 0) return 0;

    sort(ds, ds + len, mycmp);

    int v = 0;

    for (int i=0; i<len; i++) {

        v = v * 10 + ds[i];    

    }

    return ensure(v);

}



int main() {

    int n;

    scanf("%d", &n);

    

    int desc = sort_digits(n);

    int asc = reverse(desc);

    int res = desc - asc;

    print_equal(desc, asc, res);

    

    while (res != 6174 && res != 0) {

        desc = sort_digits(res);

        asc = reverse(desc);

        res = desc - asc;

        print_equal(desc, asc, res);

    }

    system("pause");

    return 0;    

}

 

你可能感兴趣的:(number)