#include <iostream>
#include<algorithm>
using namespace std;
int turn(int n)
{
int q1=n%10;
int q2=(n%100)/10;
int q3=(n%1000)/100;
int q4=n/1000;
return q4+q3*10+q2*100+q1*1000;
}
int f(int n)
{
int flag=2;
int q1=n%10;
int q2=(n%100)/10;
int q3=(n%1000)/100;
int q4=n/1000;
if(q1==q2&&q1==q3&&q1==q4)
{
flag=1;
}
return flag;
}
int ver(int n)
{
int q[4];
q[0]=n%10;
q[1]=(n%100)/10;
q[2]=(n%1000)/100;
q[3]=n/1000;
sort(q,q+4);
return q[0]+q[1]*10+q[2]*100+q[3]*1000;
}
int main()
{
int n;
cin>>n;
n=ver(n);
int t=1;
if(f(n)==1)
{
cout<<n%10<<n%10<<n%10<<n%10<<" - "<<n%10<<n%10<<n%10<<n%10<<" = 0000";
}
else
{
while(t!=6174)
{
t=n-turn(n);
if(turn(n)<1000&&turn(n>=100))
{
cout<<n<<" - 0"<<turn(n)<<" = "<<t<<endl;
}
else if(turn(n)<100&&turn(n>=10))
{
cout<<n<<" - 00"<<turn(n)<<" = "<<t<<endl;
}
else if(turn(n)<10)
{
cout<<n<<" - 000"<<turn(n)<<" = "<<t<<endl;
}
else
{
cout<<n<<" - "<<turn(n)<<" = "<<t<<endl;
}
n=ver(t);
}
}
return 0;
}
有错
#include <iostream>
#include <algorithm>
using namespace std;
int compare(const int &a, const int &b){
return a>b;
}
int main(){
int n;
scanf("%d", &n);
if(n % 1111 == 0){
printf("%d - %d = 0000\n", n, n);
return 0;
}
else{
int tmp = n;
while(true){
char c[5] = {'0','0','0','0','\0'};
int a, b;
for(int i = 3; tmp != 0; i--){
c[i] = (tmp % 10) + 48;
tmp /= 10;
}
sort(c, c + 4, compare);
a = (c[0] - 48) * 1000 + (c[1] - 48) * 100 + (c[2] - 48) * 10 + (c[3] - 48);
printf("%s - ", c);
sort(c, c + 4);
b = (c[0] - 48) * 1000 + (c[1] - 48) * 100 + (c[2] - 48) * 10 + (c[3] - 48);
printf("%s = ", c);
tmp = a - b;
printf("%04d\n", tmp);
if(tmp == 6174){
break;
}
}
}
return 0;
}