题目描述
知识点: 模拟
思路: 按照题目意思模拟即可,注意没有4位数字的需要补零进行处理排序。
#include
#include
#include
#include
using namespace std;
string my_sort(string n,int flag){
vector<char> s;
for(auto i : n)
s.push_back(i);
sort(s.begin(),s.end());
string res = "";
if(flag == 0){
for(auto i : s)
res += i;
}else{
for(int i = s.size()-1;i >= 0;i--)
res += s[i];
}
return res;
}
int main(){
string n;
cin>>n;
while(n.length() < 4)
n = '0' + n;
int s1 = stoi(my_sort(n,0));
int s2 = stoi(my_sort(n,1));
printf("%04d - %04d = %04d\n",s2,s1,s2-s1);
while(s2 - s1 != 6174 && s2 - s1 != 0){
int c = s2-s1;
string y = to_string(c);
while(y.length() < 4)
y = '0' + y;
s1 = stoi(my_sort(y,0));
s2 = stoi(my_sort(y,1));
printf("%04d - %04d = %04d\n",s2,s1,s2-s1);
}
return 0;
}