PATbasic1005

#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;

int compare(int a, int b) {
	return a > b;
}

void function(vector<int> &vec) {
	unordered_map<int,int> m;
	int i, j, a;
	for (i = 0; i < vec.size(); i++) {
		j = i;
		a = vec[i];
		while (a!=1) {
			if (a % 2) {
				m[(3 * a + 1) / 2] = j;
				a = (3 * a + 1) / 2;
			}
			else {
				m[a / 2] = j;
				a /= 2;
			}
			j++;
		}
	}
	vector<int> target;
	for (i = 0; i < vec.size(); i++) {
		if (m.find(vec[i]) == m.end()) {
			target.push_back(vec[i]);
		}
	}
	sort(target.begin(), target.end(), compare);
	i = 0;
	while (i != target.size()) {
		cout << target[i];
		if (i != target.size() - 1) cout << " ";
		i++;
	}
	cout << endl;
}

int main()
{
	vector<int> vec;//6,7
	int n, i;
	cin >> n;
	for (i = 0; i < n; i++) {
		int a;
		cin >> a;
		vec.push_back(a);
	}
	function(vec);
    return 0;
}

思路很简单,就是把所有被覆盖了的数放在map里,然后在map中查找vec[i],若没找到则vec[i]是“关键数字”。需要注意的是while(a != 1),因为比如a=3时我们必须分别验证5((3*3+1)/2),8((5*3+1)/2),4(8/2),2(4/2),1(2/2)。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值