天天看点

【PAT乙级】1019 数字黑洞 (20 分)

【PAT乙级】1019 数字黑洞 (20 分)

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

注意补前导零。

例: 输入 9 应为0009 不然 9-9=0 就直接输出就不对了

#include<bits/stdc++.h>
using namespace std;
int main(void)
{
	string a,b,temp; cin>>a;
	while(1)
	{
		while(a.size()<4) a="0"+a;
		sort(a.begin(),a.end());
		b=a;
		reverse(a.begin(),a.end());
		int a1=stoi(a);
		int b1=stoi(b);
		temp=to_string(a1-b1);
		while(temp.size()<4) temp="0"+temp;
		cout<<a<<" - "<<b<<" = "<<temp<<endl;
		if(temp=="0000") break;
		if(temp=="6174") break;
		a=temp; 
	}
	return 0;
}
           

继续阅读