• 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 this manner we will soon end up at the number 6174 -- the black hole of 4-digit numbers. This number is named Kaprekar Constant.

    For example, start from 6767, we'll get:

    1. 7766 - 6677 = 1089
    2. 9810 - 0189 = 9621
    3. 9621 - 1269 = 8352
    4. 8532 - 2358 = 6174
    5. 7641 - 1467 = 6174
    6. ... ...

    Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

    Input Specification:

    Each input file contains one test case which gives a positive integer N in the range (0,104).

    Output Specification:

    If all the 4 digits of N are the same, print in one line the equation N - N = 0000. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

    Sample Input 1:

    6767
    

    Sample Output 1:

    1. 7766 - 6677 = 1089
    2. 9810 - 0189 = 9621
    3. 9621 - 1269 = 8352
    4. 8532 - 2358 = 6174

    Sample Input 2:

    2222
    

    Sample Output 2:

    2222 - 2222 = 0000
    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int a[4], b[4];
    6. int main() {
    7. int n, t, x = 0, y = 0;
    8. cin >> n;
    9. do {
    10. x = 0, y = 0;
    11. a[0] = n % 10;
    12. a[1] = n / 10 % 10;
    13. a[2] = n / 100 % 10;
    14. a[3] = n / 1000;
    15. t = a[0];
    16. if (a[0] == t && a[1] == t && a[2] == t && a[3] == t) {
    17. cout << n << " - " << n << " = 0000";
    18. return 0;
    19. }
    20. for (int i = 0; i < 4; i++) {
    21. b[i] = a[i];
    22. }
    23. sort(a, a + 4);
    24. sort(b, b + 4, greater<int>());
    25. for (int i = 0; i < 4; i++) {
    26. x = x * 10 + a[i];
    27. y = y * 10 + b[i];
    28. }
    29. cout << b[0] << b[1] << b[2] << b[3] << " - " << a[0] << a[1] << a[2] << a[3] << " = " << setfill('0') << setw(
    30. 4) << y - x << endl;
    31. n = y - x;
    32. } while (n != 6174);
    33. return 0;
    34. }

     

  • 相关阅读:
    2022年9月电子学会Python等级考试试卷(三级)答案解析
    最全!2024百度Spring Zuul面试题大全,详解每个角落,面试必备宝典!收藏版!
    stable diffusion中的negative prompt是如何工作的
    全志R128驱动OLED屏幕步骤教程
    Array and Set work process
    vs中集成vcpkg
    dubbo分布式日志调用链追踪
    SVN 服务器建立
    智能创新,竞技未来!1024 程序员节大赛火热进行中
    SQL-DCL
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126442510