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:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
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:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 - 2222 = 0000
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个 (0,104) 区间内的正整数 N。
输出格式:
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。
输入样例 1:
6767
输出样例 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
输入样例 2:
2222
输出样例 2:
2222 - 2222 = 0000
题目分析
将给出数字的各位按从大到小排序得到数A,再将各位按从小到大排序得到数B,计算A-B得到数C,对数C重复上面的操作,直到得到数字0或者6174.
思路分析
一.将int类型整数转化成数组
void to_array(int n, int num[]) { // 将给定数字n的每一位存到num数组中
for (int i = 0; i < 4; i++) {
num[i] = n % 10;
n /= 10;
}
}
二.对数组的各位进行排序
1)从大到小
bool cmp(int a, int b) { // 从大到小排序
return a > b;
}
... ...
sort(num, num + 4, cmp); // 从大到小排序
由于sort函数将数组从小到大排序,所以需要自行设定一个cmp,获取从大到小的数组。
2)从小到大
sort(num, num + 4); // 从小到大排序
正常使用sort即可。
三.将数组转化成整数,并作差得到新的数字
int to_number(int num[]) { // 将数组转化成数字
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = sum * 10 + num[i];
}
return sum;
}
四.细枝末节(重点!易忽视)
printf("%04d - %04d = %04d\n", max, min, n);
//注意:这里需要考虑首位为0的情况,所以最好使用"%04d"来确保max和min都是四位数
如果在“将数组转化成数字”的过程对首位为0的情况进行处理那就太麻烦了,不如使用C语言printf"%04d"将输出数字固定为4位,位数不足则使用0在首位进行补位的方法更方便。
"%04d"其中:
-
%: 指定这是一个格式说明符的开始 -
0: 表示用零填充空缺的位数 -
4: 表示输出的总宽度为 4 个字符。如果实际数字的位数少于 4,则会在前面填充零 -
d: 表示要输出的是一个整数
类似的用法还有
printf("%5d\n", 42); // 输出: " 42"
-
"%5d": 输出宽度为 5 的整数,如果不足 5 位,则在前面用空格填充
printf("%05d\n", 42); // 输出: "00042"
-
"%05d": 输出宽度为 5 的整数,如果不足 5 位,则在前面用零填充。
printf("%4.2f\n", 3.14159); // 输出: " 3.14"
-
"%4.2f": 输出宽度为 4 的浮点数,保留 2 位小数
printf("%-4d\n", 42); // 输出: "42 "
-
"%-4d": 左对齐输出宽度为 4 的整数
printf("%x\n", 255); // 输出: "ff"
-
"%x": 以十六进制格式输出整数
五.实现代码
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) { // 从大到小排序
return a > b;
}
void to_array(int n, int num[]) { // 将给定数字n的每一位存到num数组中
for (int i = 0; i < 4; i++) {
num[i] = n % 10;
n /= 10;
}
}
int to_number(int num[]) { // 将数组转化成数字
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = sum * 10 + num[i];
}
return sum;
}
int main() {
int n, min, max; // min为递减排序的值,max为递增排序的值
scanf("%d", &n);
int num[5];
while (true) {
to_array(n, num); // 数字转换成数组
sort(num, num + 4); // 从小到大排序
min = to_number(num);
sort(num, num + 4, cmp); // 从大到小排序
max = to_number(num);
n = max - min;
printf("%04d - %04d = %04d\n", max, min, n);
//注意:这里需要考虑首位为0的情况,所以最好使用"%04d"来确保max和min都是四位数
if (n == 0 || n == 6174) break;
}
return 0;
}
如有错误或不足,欢迎斧正!
1983

被折叠的 条评论
为什么被折叠?



