注意不满\(4\)位的话要补成\(4\)位。
string s;
set<int> S;
int main()
{
cin>>s;
while(s.size()<4) s='0'+s;
while(true)
{
sort(s.begin(),s.end(),greater<char>());
string sa=s;
reverse(s.begin(),s.end());
string sb=s;
int a=stoi(sa),b=stoi(sb);
int res=a-b;
if(S.count(res)) break;
else
{
printf("%04d - %04d = %04d\n",a,b,res);
S.insert(res);
s=to_string(res);
while(s.size()<4) s='0'+s;
}
}
//system("pause");
return 0;
}