题目来源[The Black Hole of Numbers - 牛客](The Black Hole of Numbers)The Black Hole of Numbers - PTA注意点输入可能不是 4 位数输入可能是 6174DescriptionFor any4 44-digit integer except the ones with all the digits being thesame, 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 number6174 61746174– the “black hole” of4 44-digit numbers. This number is namedKaprekar Constant. For example, start from6767 67676767, we’ll get:7766 - 6677 1089 9810 - 0189 9621 9621 - 1269 8352 8532 - 2358 6174 7641 - 1467 6174 ... ...Given any4 44-digit number, you are supposed to illustrate the way itgets into the black hole.Input SpecificationEach input file contains one test case which gives a positive integerN NNin the range[ 0 , 10 4 ) [0, 10^4)[0,104).Output SpecificationIf all the4 44digits ofN NNare the same, print in one line the equationN - N 0000. Else print each step of calculation in a line until6174 61746174comes out as the difference. All the numbers must be printed as4 44-digit numbers.Sample Input 16767Sample Output 17766 - 6677 1089 9810 - 0189 9621 9621 - 1269 8352 8532 - 2358 6174Sample Input 22222Sample Output 22222 - 2222 0000题目大意给一个小于10 4 10^4104的整数将这个整数的每个位从大到小排序得到a aa从小到大排序得到b bb不断输出a - b c对c重复操作直到其等于6174 61746174思路简介如字面意思但是有几个易错点输入的数不一定是四位数要补零到四位数在相减过程中输出的每个数都必须是四位数输入可能直接是6174 61746174这时应当再运算一步7641 - 1467 6174而不是直接不处理每个位都相同输出N - N 0000遇到的问题输入时没补齐输入6174 61746174时没进行处理代码/** * The Black Hole of Numbers * https://www.nowcoder.com/pat/5/problem/4030 * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId994805400954585088 * 模拟 */#includebits/stdc.husingnamespacestd;stringformat(string s){returnstring(4-s.size(),0)s;}voidsolve(){string s;cins;sformat(s);if(s[0]s[1]s[1]s[2]s[2]s[3]){couts - s 0000\n;return;}elseif(s6174){cout7641 - 1467 6174\n;return;}while(s!6174){string s1s,s2s;sort(s1.begin(),s1.end(),[](chara,charb){returnab;});sort(s2.begin(),s2.end());sto_string(stoi(s1)-stoi(s2));sformat(s);couts1 - s2 s\n;}}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//fstream in(in.txt,ios::in);cin.rdbuf(in.rdbuf());intT1;//cinT;while(T--){solve();}return0;}