Make Them Odd CodeForces - 1277B

一、内容

题意:将所有的偶数都变成奇数,问最少需要多少步。每次选定一个偶数,然后所有相同的数都除2.

二、思路

  • 如果当前的数不是偶数,就用map记录当前偶数,如果已经访问过就不用再除2了,如果没有访问过那么就一直除2。

三、代码

#include <cstdio>
#include <unordered_map>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5;
int t, num, n, ans;
unordered_map<int, bool> mp;
int main() {
	scanf("%d", &t);
	while (t--) {
		mp.clear();
		ans = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &num);
			if (num % 2 == 0) {
				while (num % 2 == 0) {
					//代表这类的偶数都除过了 
					if (mp[num]) break;
					mp[num] = true;
					num /= 2;
					ans++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
} 
发布了343 篇原创文章 · 获赞 244 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/103585249
今日推荐