[ACM]6174问题

//
//  main.cpp
//  testC++
//

#include <iostream>
//6174问题
using namespace std;
int num[2000];

int get_next(int x){
    int a, b = 0, n;
    char s[10];
    sprintf(s, "%d", x);
    n = (int)strlen(s);
    //bubble sort ,get min
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if(s[i] > s[j]){
                char t = s[i];
                s[i] = s[j];
                s[j] = t;
            }
        }
    }
    sscanf(s, "%d", &b);
    //reverse
    for(int i = 0; i < n / 2; i++){
        char t = s[i];
        s[i] = s[n - i - 1];
        s[n - i - 1] = t;
    }
    sscanf(s, "%d", &a);
    return a - b;
}

int main(int argc, const char * argv[])
{
    scanf("%d", &num[0]);
    printf("%d", num[0]);
    int count = 1;
    for (; ;) {
        num[count] = get_next(num[count - 1]);
        printf(" -> %d", num[count]);
        int found = 0;
        for(int i = 0; i < count; i++)
            if (num[i] == num[count]) {
                found = 1;
                break;
            }
        if(found)
            break;
        count++;
    }
    printf("\n");
    return 0;
}


你可能感兴趣的:(c,ACM)