就是输出处理比较烦人,测试点2,3,4考虑输入一位,两位,三位数的情况!
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int N,max,min,c = 1; //max瀛樻斁鐢卞ぇ鍒板皬鐨勬暟锛宮in瀛樻斁鐢卞皬鍒板ぇ鐨勬暟锛宑瀛樻斁宸?
int i,z,j;
int a[4];
cin>>N;
while(c != 6174 && c != 0)
{
max = 0;
min = 0;
for(i = 0;i < 4;i++) //杞寲鎴愭瘡涓€浣?
{
a[i] = N%10;
N /= 10;
}
sort(a,a+4);
for(i=3;i>=0;i--)
{
max = max*10 + a[i];
}
for(i=0;i<4;i++)
{
min = min*10 + a[i];
}
c = max-min;
N = c;
if(c==0 && a[0]==0 && a[1]==0 && a[2]==0 && a[3]==0)
cout<<"0000"<<" - "<<"0000"<<" = "<<"0000"<<endl;
else if(c==0)
cout<<max<<" - "<<min<<" = "<<"0000"<<endl;
else if(c < 1000 && a[0]==0 && a[1]==0 && a[2]==0)
cout<<max<<" - "<<"000"<<min<<" = "<<"0"<<c<<endl;
else if(a[0]==0 && a[1]==0 && a[2]==0)
cout<<max<<" - "<<"000"<<min<<" = "<<c<<endl;
else if(a[0]==0 && a[1]==0 )
cout<<max<<" - "<<"00"<<min<<" = "<<c<<endl;
else if(a[0]==0)
cout<<max<<" - "<<"0"<<min<<" = "<<c<<endl;
else
cout<<max<<" - "<<min<<" = "<<c<<endl;
}
return 0;
}