1069 The Black Hole of Numbers

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1069 The Black Hole of Numbers相关的知识,希望对你有一定的参考价值。

注意两点:

1. 不足4位要补足,不仅仅是一开始要考虑,每次得到一个差值,都要考虑

2. 到0也会停下,不仅仅是一开始可能发生,也可能是过程中的某一个差值

另:

vector<int> 是可以作为函数的参数的。

AC代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<map>

using namespace std;
typedef long long LL;

const int maxn = 50007;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界  
const LL SUP = (1LL<<63)-1;//SUP:上确界 
const double eps = 1e-5;

int getNum(vector<int> vi){
	int ans = 0;
	for(int i=vi.size()-1;i>=0;i--){
		ans = ans*10+vi[i];
	}
	return ans;	
}

int main(){
	string s;
	vector<int> vi; 
	cin>>s;
	
	while(s.length()!=4){//不足4位补零 
		s+="0";	
	}
	
	for(int i=0;i<s.length();i++){
		vi.push_back(s[i]-'0');
	}
	
	sort(vi.begin(),vi.end());

	int big = getNum(vi);
	reverse(vi.begin(),vi.end());
	int small = getNum(vi);
	int sub = big - small;
	
	while(sub!=6174&&sub!=0){
		printf("%04d - %04d = %04d\\n",big,small,sub);
		vi.clear();
		
		while(sub){
			vi.push_back(sub%10);
			sub /= 10;
		}
		while(vi.size()!=4){//不足4位补零
			vi.push_back(0);
		}	
		sort(vi.begin(),vi.end());	
		
		big = getNum(vi);
		reverse(vi.begin(),vi.end());
		small = getNum(vi);
		sub = big - small;
	}
	printf("%04d - %04d = %04d\\n",big,small,sub);
	
	return 0;
}

以上是关于1069 The Black Hole of Numbers的主要内容,如果未能解决你的问题,请参考以下文章

PAT1069:The Black Hole of Numbers

pat 1069 The Black Hole of Numbers(20 分)

PAT1069. The Black Hole of Numbers

PAT Advanced 1069 The Black Hole of Numbers (20分)

1069. The Black Hole of Numbers (20)模拟——PAT (Advanced Level) Practise

1069 The Black Hole of Numbers