题目:http://pat.zju.edu.cn/contests/pat-a-practise/1069
思路:水题
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool cmp1(int a,int b)
{
if(a>b) return true;
return false;
}
bool cmp2(int a,int b)
{
if(a<b) return true;
return false;
}
int main()
{
int i,n,a[4];
scanf("%d",&n);
a[3]=n%10;
a[2]=(n/10)%10;
a[1]=(n/100)%10;
a[0]=n/1000;
if((a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])) printf("%04d - %04d = 0000\n",n,n);
else
{
while(true)
{
sort(a,a+4,cmp1);
int num1=a[0]*1000+a[1]*100+a[2]*10+a[3];
sort(a,a+4,cmp2);
int num2=a[0]*1000+a[1]*100+a[2]*10+a[3];
printf("%04d - %04d = %04d\n",num1,num2,num1-num2);
if(num1-num2==6174) break;
num1=num1-num2;
a[3]=num1%10;
a[2]=(num1/10)%10;
a[1]=(num1/100)%10;
a[0]=num1/1000;
}
}
return 0;
}