很有趣的一道题,我也是看了别人的题解才懂的,所以我就不写题解了,去看别人的吧23333
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int t;
long long a;
long long ans;
int main() {
scanf("%d", &t);
while (t--) {
ans = 0;
scanf("%lld", &a);
while (a > 0) {
if (a & 1) {
ans++;
}
a >>= 1;
}
printf("%lld\n", (long long)pow(2, ans));
}
//system("pause");
return 0;
}
本文介绍了一种使用位运算计算2的幂次方的方法。通过不断右移和检查最低位是否为1,计数得到1的个数,最终利用pow函数计算2的该幂次方值。代码使用C++实现,适用于处理二进制位操作和数学计算的场景。
841

被折叠的 条评论
为什么被折叠?



