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 |
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:
1 | 6767 |
Sample Output 1:
1 | 7766 - 6677 = 1089 |
Sample Input 2:
1 | 2222 |
Sample Output 2:
1 | 2222 - 2222 = 0000 |
题意
6174是一个黑洞数字,所有的4位数(除了4个数字一样的),经过降序,再减去升序,得到的结果再重复这个过程,最终的数字都会得到6174,现在要求把这个过程展现出来.
注意这里有一个坑,那就是不管你输入的数是不是4位数,展现过程中都必须显示为4位,也就是说不足4位的前面要补零.
思路
这道题的难点也不是很高,只是要知道上面说的那个坑,把所有的数都补足4位就行了.
代码
创建一个给字符串降序排序的函数:
1 | public static string decreasingSort(string input) |
创建一个反转字符串的函数:
1 | public static string Reverse(string input) |
1 | public static void Main() |