思路
- 以字符串接收数据
- sort():数组排序,倒序排序,重新写比较函数
- s.insert(0, 4 – s.length(), ‘0’):用来给不足4位的时候前面补0
- do while语句,输入6174时也计算一次
#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');
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;
}
参考
https://www.liuchuo.net/archives/541