• 1019 数字黑洞


    一.问题:

    给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。

    例如,我们从6767开始,将得到

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

    现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。

    输入格式:

    输入给出一个 (0,104) 区间内的正整数 N。

    输出格式:

    如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。

    输入样例 1:

    6767
    

    输出样例 1:

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

    输入样例 2:

    2222
    

    输出样例 2:

    2222 - 2222 = 0000

    二.思路:

            直接看代码。

    三.代码实现(C语言实现):

    1. #include
    2. // 获取每个位上的数字
    3. void get_nums(int num, int nums_arr[4])
    4. {
    5. int qian = num / 1000;
    6. nums_arr[0] = qian;
    7. nums_arr[1] = (num % 1000) / 100;
    8. nums_arr[2] = (num % 100) / 10;
    9. nums_arr[3] = num % 10;
    10. }
    11. // 还原一个四位数
    12. int restore_num(int *nums_arr)
    13. {
    14. int qian = nums_arr[0] * 1000;
    15. int bai = nums_arr[1] * 100;
    16. int shi = nums_arr[2] * 10;
    17. int ge = nums_arr[3];
    18. return (qian + bai + shi + ge);
    19. }
    20. // 对数组元素进行快速排序,option为1时递增,为-1时递减
    21. void sort_func(int left, int right, int *nums_arr, int option)
    22. {
    23. int i = left;
    24. int j = right;
    25. int temp = nums_arr[left];
    26. int t;
    27. if (i > j)
    28. {
    29. return;
    30. }
    31. while (i != j)
    32. {
    33. if (option == 1)
    34. {
    35. while (nums_arr[j] >= temp && i < j)
    36. {
    37. j--;
    38. }
    39. while (nums_arr[i] <= temp && i < j)
    40. {
    41. i++;
    42. }
    43. }
    44. else if (option == -1)
    45. {
    46. while (nums_arr[j] <= temp && i < j)
    47. {
    48. j--;
    49. }
    50. while (nums_arr[i] >= temp && i < j)
    51. {
    52. i++;
    53. }
    54. }
    55. if (i < j)
    56. {
    57. t = nums_arr[i];
    58. nums_arr[i] = nums_arr[j];
    59. nums_arr[j] = t;
    60. }
    61. }
    62. nums_arr[left] = nums_arr[i];
    63. nums_arr[i] = temp;
    64. sort_func(left, i - 1, nums_arr, option);
    65. sort_func(i + 1, right, nums_arr, option);
    66. }
    67. int main()
    68. {
    69. // 输入数字
    70. int N = 0;
    71. scanf("%d", &N);
    72. // 拆分数字
    73. int nums[4] = {0};
    74. get_nums(N, nums);
    75. // 检查 N 的 4 位数字是否全相等,若全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现
    76. if ((nums[0] == nums[1]) && (nums[1] == nums[2]) && (nums[2] == nums[3]))
    77. {
    78. printf("%04d - %04d = 0000\n", N, N);
    79. }
    80. else
    81. {
    82. // 开始运算
    83. int result = 0;
    84. while (result != 6174)
    85. {
    86. // 1.获取非递增排序的四位正整数
    87. sort_func(0, 3, nums, -1);
    88. int A = restore_num(nums);
    89. // 2.获取非递减排序的四位正整数
    90. sort_func(0, 3, nums, 1);
    91. int B = restore_num(nums);
    92. // 3.相减运算
    93. result = A - B;
    94. printf("%04d - %04d = %04d\n", A, B, result);
    95. // 对新数字处理,使其重复上述循环处理
    96. get_nums(result, nums);
    97. }
    98. }
    99. return 0;
    100. }

  • 相关阅读:
    wireshark图形界面介绍
    90---Python 直角坐标系下绘制椭圆形
    美国网站服务器解决方案
    java毕业生设计高校共享机房管理系统的设计与实现计算机源码+系统+mysql+调试部署+lw
    ERR_PNPM_JSON_PARSE Unexpected end of JSON input while parsing empty string in
    Java.lang.Class类 getField(name)方法有什么功能呢?
    前端面试要点
    Unity技术手册-脚本初始(上)
    leetcode 剑指offer 19:正则表达式匹配
    Quartz核心原理之架构及基本元素介绍
  • 原文地址:https://blog.csdn.net/2303_76295261/article/details/132953292