PTA乙级 (*1019 数字黑洞 (20分))

1019 数字黑洞 (20分)

https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
	string str;
	int a[4]={0,0,0,0};
	cin>>str;
	int len=str.length();
	for(int i=0;i<len;i++) a[i]=str[i]-'0';
	int d=0,x=0,sub=0;
	if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[3]==a[1]) 
	cout<<str<<" - "<<str<<" = "<<"0000"<<endl;
	else{
		while(sub!=6174)
		{
			sort(a,a+4);
			for(int i=0;i<4;i++)
			{
				d=10*d+a[i];
				x=10*x+a[3-i];
			}
			sub=x-d;
			printf("%04d - %04d = %04d\n",x,d,sub);
			a[3]=sub%10;
			a[2]=(sub/10)%10;
			a[1]=(sub/100)%10;
			a[0]=(sub/1000)%10;
			d=0;
			x=0;
		}
	}
	return 0;
}
posted @ 2020-02-01 19:22  yyer  阅读(235)  评论(0编辑  收藏  举报