#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
bool cmp(int a, int b){ return a>b; }
char n[4]={'0','0','0','0'};
int main(){
int i, len, temp1,temp2,sub=0,loop=0;
int tp1[4]={0}, tp2[4]={0};
scanf("%s",n);
if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])printf("%s - %s = 0000\n",n,n);
else {
len=strlen(n);for(i=0;i<len;i++)tp1[i]=n[i]-'0'; for(i=0;i<len;i++)tp2[i]=n[i]-'0';
while(1){
sort(tp1,tp1+4,cmp); //从大到小
temp1=1000*tp1[0]+100*tp1[1]+10*tp1[2]+tp1[3];
sort(tp2,tp2+4); //从小到大
temp2=1000*tp2[0]+100*tp2[1]+10*tp2[2]+tp2[3];
sub=temp1-temp2;
printf("%d%d%d%d - %d%d%d%d = %04d\n",tp1[0],tp1[1],tp1[2],tp1[3],tp2[0],tp2[1],tp2[2],tp2[3],sub);
if(sub==6174)break;
tp1[0]=tp2[0]=sub%10;sub/=10; tp1[1]=tp2[1]=sub%10;sub/=10;
tp1[2]=tp2[2]=sub%10;sub/=10; tp1[3]=tp2[3]=sub%10;sub/=10;
}
}
return 0;
}
PAT-A1069
最新推荐文章于 2022-05-01 10:59:11 发布