1064 - I am Two
Time Limit:1s Memory Limit:64MByte
Submissions:522Solved:205
DESCRIPTION
Check whether an integer n is a power of 2.
INPUT
First line contains a single integer T (T<=20) which denotes the number of test cases. For each test case, there is an 32-bit integer N .
OUTPUT
For each case, output the "Yes" or "No" in a single line.
SAMPLE INPUT
3138
SAMPLE OUTPUT
YesNoYes
SOLUTION
code:
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n>0&&(n&(n-1))==0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

本文介绍了一个简单的算法来检查一个整数是否为2的幂次。通过输入一个32位整数N,如果N大于0且N与N-1的按位与运算结果为0,则N是2的幂次,否则不是。

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



