题目
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 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
,
1
0
4
0,10^4
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
翻译
1019 数字黑洞 (20 分)
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个 ( 0 , 1 0 4 0,10^4 0,104 ) 区间内的正整数 N。
输出格式:
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。
思路
设置一个数组num[4]存储数字的各个位,用sort排序以后得到排序后的数字,循环求得最大最小,相减,输出,重复上述步骤。
初次代码
#include<cstdio>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
int num[4];
void getList(int n) {
int i = 4;
while (i--) {
num[i] = n % 10;
n /= 10;
}
}
int main() {
int n;
scanf("%d", &n);
while (n != 6174) {
getList(n);
sort(num, num + 4);
int a = 0, b = 0;//a<b
for (int i = 0; i < 4; i++) {
b = b * 10 + num[3 - i];
a = a * 10 + num[i];
}
n = b - a;
printf("%04d - %04d = %04d\n", b, a, n);
}
return 0;
}
Note
不足四位补零%04d.
得分:
2019/4/16 16:15:33
部分正确
17 1019 C++ (g++) 2 ms
0 运行超时 0 ms 0 KB
1 答案正确 2 ms 240 KB
2 答案正确 1 ms 260 KB
3 答案正确 2 ms 384 KB
4 答案正确 1 ms 384 KB
5 答案错误 1 ms 356 KB
6 答案正确 2 ms 256 KB
错误
- n为0时需退出循环,while内条件添上n&&以后0测试点通过.
- 少考虑了一种情况,即n一开始就为6174,这个还是可以输出一个式子的,所以单独为一个判断条件。加上
if(n==6174){
printf("7641 - 1467 = 6174");
return 0;
}
即可.
改正代码
#include<cstdio>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
int num[4];
void getList(int n) {
int i = 4;
while (i--) {
num[i] = n % 10;
n /= 10;
}
}
int main() {
int n;
scanf("%d", &n);
if(n==6174){
printf("7641 - 1467 = 6174");
return 0;
}
while (n&&n != 6174) {
getList(n);
sort(num, num + 4);
int a = 0, b = 0;//a<b
for (int i = 0; i < 4; i++) {
b = b * 10 + num[3 - i];
a = a * 10 + num[i];
}
n = b - a;
printf("%04d - %04d = %04d\n", b, a, n);
}
return 0;
}
优化
while循环条件设为1,在循环内判断结束.
可以省掉上述错误1.
#include<cstdio>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
int num[4];
void getList(int n) {
int i = 4;
while (i--) {
num[i] = n % 10;
n /= 10;
}
}
int main() {
int n;
scanf("%d", &n);
while (1) {
getList(n);
sort(num, num + 4);
int a = 0, b = 0;//a<b
for (int i = 0; i < 4; i++) {
b = b * 10 + num[3 - i];
a = a * 10 + num[i];
}
n = b - a;
printf("%04d - %04d = %04d\n", b, a, n);
if (n == 0 || n == 6174) break;
}
return 0;
}
THE END
第一次发文,希望以后刷题、感悟都可以及时写上来,加油!!!