【1069 20 数学】 The Black Hole of Numbers
传送门
题意
给定一个数字\(n\),将其按照每位从大到小得到\(a\),从小到大排序后得到\(b\),当\(a-b\)的结果在进行运算时候结果不变终止
数据范围
\(n\in (0,10^{4})\)
题解
- 答案只有\(0000\)和\(6174\)这四个数字组合
- 当输入为二者的时候也要有输出,利用
do-while先执行在判断
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
// cout<<setfill('0'); // 设置空格填充符
// cout<<setw(4)<<a; // 表示a不够四位补空格
string s; cin >> s;
s.insert(0, 4 - s.size(), '0');
do {
string a = s, b = s;
sort(a.begin(), a.end(), greater<char>());
sort(b.begin(), b.end());
int res = stoi(a) - stoi(b);
s = to_string(res);
s.insert(0, 4 - s.size(), '0');
cout << a << " - " << b << " = " << s << endl;
} while (s != "6174" && s != "0000");
return 0;
}

浙公网安备 33010602011771号