PAT数学规律题---1019 数字黑洞 (20分)

1019 数字黑洞 (20分)

  • 一个字符串的写法,坑点就是可能会出现不到4位的数,进行补零
#include<iostream>
#include<vector>
#include<cctype>
#include<map>
#include<set>
#include<sstream>
#include<string>
#include<cstdio>
#include<algorithm>

#define inf 0x3f3f3f3f

const int maxn=10005;
typedef long long ll;

using namespace std;

int upSort(string s) {
	sort(s.begin(),s.end(),greater<int>());
	int num=stoi(s);
}
int downSort(string s) {
	sort(s.begin(),s.end());
	int num=stoi(s);
}
string change(string s){
	int cnt=s.size();
	if(cnt<4) {
		for(int i=0; i<4-cnt; i++) {
			s='0'+s;
		}
	}
	return s;
}
int main() {
	string s;
	cin>>s;
	s=change(s);
	int a=upSort(s),b=downSort(s);
	int c=a-b;
	if(!c) printf("%04d - %04d = 0000\n",a,a);
	while(c) {
		printf("%04d - %04d = %04d\n",a,b,c);
		if(c==6174) break;
		string s1=to_string(c);
		s1=change(s1);
		a=upSort(s1),b=downSort(s1);
		c=a-b;
	}
	return 0;
}

posted @ 2020-06-15 13:36  chstor  阅读(242)  评论(0编辑  收藏  举报