PAT散列题---1005 继续(3n+1)猜想 (25分)

1005 继续(3n+1)猜想 (25分)

  • 输入的数列里面,输出“关键数”(没被覆盖的)
  • 标记一下n往后的就可以了,标记的是覆盖的
  • eg:{3、5、8、4、2、1},标记{5、8、4、2}
#include<iostream>
#include<vector>
#include<cctype>
#include<map>
#include<set>
#include<sstream>
#include<string>
#include<cstdio>
#include<algorithm>

const int maxn=10005;
using namespace std;

int cnt[maxn];
int main() {
	int n;cin>>n;
	int arr[maxn];
	for(int i=0;i<n;i++){
		int x;cin>>x;
		arr[i]=x;
		while(x!=1){
			if(x%2){
				x=3*x+1;
			}
			x/=2;
			if(cnt[x]==1) break;
			cnt[x]=1;
		}
	}
	sort(arr,arr+n);
	int flag=0;
	for(int i=n-1;i>=0;i--){
		if(!cnt[arr[i]]){
			if(flag) cout<<" ";
			cout<<arr[i],flag=1; 
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/bingers/p/13095587.html