AcWing 1555. 数字黑洞
原题链接
简单
作者:
巨鹿噜噜噜路
,
2020-05-31 22:50:09
,
所有人可见
,
阅读 721
C++ 代码
#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
#include <algorithm>
using namespace std;
void PrintNum(vector<int>& num) {
for (auto& i : num) {
printf("%d", i);
}
}
void PrintEq(vector<int>& a, vector<int>& b, vector<int>& c) {
PrintNum(a);
printf(" - ");
PrintNum(b);
printf(" = ");
PrintNum(c);
printf("\n");
}
int main() {
int n;
cin >> n;
vector<int> num, iNum, dNum, tmp;
for (int i = 0; i < 4; i++) {
num.push_back(n % 10);
n /= 10;
}
int t = 0;
int isFrist = true;
while (true) {
tmp = num;
iNum = num;
dNum = num;
sort(iNum.begin(), iNum.end());
sort(dNum.begin(), dNum.end(), greater<int>());
for (int i = 3; i >= 0; i--) {
bool flag = false;
t = dNum[i] - iNum[i] - t;
if (t < 0) {
t += 10;
flag = true;
}
num[i] = t % 10;
if (flag) t = 1;
else t = 0;
}
if (!isFrist && tmp == num) break;
PrintEq(dNum, iNum, num);
isFrist = false;
}
return 0;
}