PAT-B1019

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值