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, 10000).
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
我的AC代码(刚开始刷PAT,很菜):
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int sort(int x) {
int t[4];
for (int i = 0; i < 4; i++) {
t[i] = x % (int)round(pow(10, i + 1)) / (int)round(pow(10, i));
x = x - t[i]* (int)round(pow(10, i));
}
int temp;
int m, n;
for (m = 0; m < 3; m++) {
for (n = 0; n < 3 - m; n++) {
if (t[n] < t[n + 1]) {
temp = t[n];
t[n] = t[n + 1];
t[n + 1] = temp;
}
}
}
return t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];
}
int nixu(int x) {
int t[4];
for (int i = 0; i < 4; i++) {
t[i] = x % (int)round(pow(10, i + 1)) / (int)round(pow(10, i));
}
return t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];
}
int main() {
int input;
cin >> input;
int num1, num2, res;
num1 = sort(input);
num2 = nixu(num1);
res = num1 - num2;
if(num1 == 0){ //当num1=0时对num1前面加0,不然输出为0-0000=0
if(num2 < 1000 && num2 >= 100){
cout << "000" << num1 << " - 0" << num2 << " = 000" << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << "000" << num1 << " - 00" << num2 << " = 000" << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << "000" << num1 << " - 000" << num2 << " = 000" << res << endl;
}
else{
cout << "000" << num1 << " - " << num2 << " = 000" << res << endl;
}
}
else if (num1 != 0 && res <= 9 & res >= 0){ //当num1!=0,对res加0,不然存在输出res不是四位数的情况
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = 000" << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = 000" << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = 000" << res << endl;
}
else{
cout << num1 << " - " << num2 << " = 000" << res << endl;
}
}
else if (num1 != 0 && res <= 99 & res > 9){
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = 00" << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = 00" << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = 00" << res << endl;
}
else{
cout << num1 << " - " << num2 << " = 00" << res << endl;
}
}
else if (num1 != 0 && res <= 999 & res > 99){
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = 0" << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = 0" << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = 0" << res << endl;
}
else{
cout << num1 << " - " << num2 << " = 0" << res << endl;
}
}
else if (num1 != 0 && res > 999){
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = " << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = " << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = " << res << endl;
}
else{
cout << num1 << " - " << num2 << " = " << res << endl;
}
}
while (num1 != num2 && res != 6174){
num1 = sort(res);
num2 = nixu(num1);
res = num1 - num2;
if(res > 999){ //当num1=0时对num1前面加0,不然输出为0-0000=0
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = " << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = " << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = " << res << endl;
}
else{
cout << num1 << " - " << num2 << " = " << res << endl;
}
}
else{
if(num2 < 1000 && num2 >= 100){
cout << num1 << " - 0" << num2 << " = 0" << res << endl;
}
else if(num2 < 100 && num2 >= 10){
cout << num1 << " - 00" << num2 << " = 0" << res << endl;
}
else if(num2 < 10 && num2 >= 0){
cout << num1 << " - 000" << num2 << " = 0" << res << endl;
}
else{
cout << num1 << " - " << num2 << " = 0" << res << endl;
}
}
}
return 0;
}
刚开始只看英文题目和输入样例,只想到了最基本的将输入数字按位取出,再进行排序和逆序处理,踩了个坑,完全没注意题目要求的四位数,一直卡在这。想了好久,不停输入测试才想到num1、num2和res都要求是四位数。
这里我是这样对输入的数据进行取出每一位的
for (int i = 0; i < 4; i++) {
t[i] = x % (int)round(pow(10, i + 1)) / (int)round(pow(10, i));
x = x - t[i]* (int)round(pow(10, i));
}
还踩了个坑,用的VSCODE,同样的输入1798,在线编译器输出结果与我的预期结果一致,sort之后都是9871,而在VSCODE上一直都是8710,怎么想都觉得我写的逻辑没错。我原来的sort()里是这样的
for (int i = 0; i < 4; i++) {
t[i] = x % (int)pow(10, i + 1) / pow(10, i);
x = x - t[i]* (pow(10, i);
}
后面所有的pow()前面都加上了int强转还是没用,最后百度到,pow(10,2)是99.999999,(int)pow(10,2)之后是99……两种解决办法:1.自己写pow()函数,2.用round()四舍五入。于是用了round()最后才得救。
最终AC之后,觉得自己的代码也太低端了…于是去找了柳神(软院考研狗的偶像)的代码学习
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(char a, char b) {return a > b;}
int main() {
string s;
cin >> s;
s.insert(0, 4 - s.length(), '0'); //用来给不足4位的时候前面补0
do {
string a = s, b = s;
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end());
int result = stoi(a) - stoi(b);
s = to_string(result);
s.insert(0, 4 - s.length(), '0');
cout << a << " - " << b << " = " << s << endl;
} while (s != "6174" && s != "0000");
return 0;
}
C++ STL:
sort(a.begin(), a.end(), cmp):将字符串a按cmp的规则对a进行排序处理
stoi():将数值类型的字符串转换为int类型。atoi()功能与stoi()相似,主要区别在于atoi()的参数是const char*,因此对于字符串str必须调用c_str()把这个string转换成const char类型的,而stoi()的参数类型是const string,不需要转化
博客围绕PAT中4位数黑洞6174的题目展开,介绍了题目内容,即对非全相同的4位数排序相减最终会得到6174。作者分享了自己的AC代码编写过程,提到遇到的两个坑,一是要注意结果为四位数,二是pow函数精度问题,最后还学习了柳神的代码及C++ STL相关知识。

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



