首页 > 其他 > 详细

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

时间:2020-06-11 19:46:50      阅读:53      评论:0      收藏:0      [点我收藏+]

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;
}


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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!