0
点赞
收藏
分享

微信扫一扫

[PAT甲] 1005 继续(3n+1)猜想 (25 分)

吴陆奇 2022-02-22 阅读 50
算法

在这里插入图片描述

#include<cstdio>
#include<algorithm>
using namespace std;
bool HashTable[100000]={false};//3n+1 会产生超过100的数
int arr[110]={0};
int main(){
	int n,i=0;
	scanf("%d",&n);
	while(n--){
		int temp;
		scanf("%d",&temp);
		arr[i++]=temp;
		while(temp!=1){
			if(temp%2==0){
				temp/=2;
			}else{
				temp=(3*temp+1)/2;
			}
			HashTable[temp]=true;
		}
	}
	int flag=0;
	sort(arr,arr+i);
	for(i=i-1;i>=0;i--){
		if(HashTable[arr[i]]) continue;
		if(!flag){
			printf("%d",arr[i]);
			flag=1;
		}else{
			printf(" %d",arr[i]);
		}
	}
}```

举报

相关推荐

0 条评论