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;
}
时间: 2024-08-04 01:50:28

PAT 1069. The Black Hole of Numbers的相关文章

PAT 1069. The Black Hole of Numbers (20)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new n

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second

PAT Advanced 1069 The Black Hole of Numbers (20分)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

PAT:1069. The Black Hole of Numbers (20) AC

#include<stdio.h> #include<algorithm> using namespace std; const int AIM=6174; int n; int arr[4]; bool NonIncreasingOrder(int a,int b) { return a>b; } bool NonDecreasingOrder(int a,int b) { return a<b; } int toNum() //得到这个数字 { int sum=0;

1069. The Black Hole of Numbers (20)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

1069. The Black Hole of Numbers

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

pat1069. The Black Hole of Numbers (20)

1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-

PAT 1069. 微博转发抽奖

PAT 1069. 微博转发抽奖 小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N和S,分别是转发的总量.小明决定的中奖间隔.以及第一位中奖者的序号(编号从1开始).随后M行,顺序给出转发微博的网友的昵称(不超过20个字符.不包含空格回车的非空字符串). 注意:可能有人转发多次,但不能中奖多次.所以如果处于当前中奖位置的网友已经中过奖,则跳过他顺次取下