无论如何都要做一次相减,所以用do{}while;循环
对string 类型进行排序sort(a.begin(),a.end(),cmp);
to_string() stoi分别用来做字符与整形之间的转换
s.insert(position,sum,value)来插入0;
这里用s.insert(0,4-s.length(),0)来在前面插入0;
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(char a,char b)
{
return a>b;}
int main()
{
string s;
int c;
cin>>s;
do{
s.insert(0,4-s.length(),'0');
string a=s;
string b=s;
sort(a.begin(),a.end());
sort(b.begin(),b.end(),cmp);
c=stoi(b)-stoi(a);
s=to_string(c);
s.insert(0,4-s.length(),'0');
cout<<b<<" - "<<a<<" = "<<s<<'\n';}
while(s!="6174"&&s!="0000");
return 0;
}