sdnu 1483 求一个大整数是否为2的次方
1483.Problem_A
Description
Check whether an integer n is a power of 2.
Input
First line contains a single integer T (T<=20000000) which denotes the number of test cases. For each test case, there is an big integer N(0 < N < 2^1000)
Output
For each case, output the "Yes" or "No" in a single line.
Sample Input
313 8
Sample Output
YesNoYes
Hint
Source
Unknown
现在~~题目给出的是一个大整数~求这个是不是2的次方~~这种情况下~我们可以选择模2取余法的模拟;
将这个大整数变成一个2进制~~如果除了最高位还有1的出现~那么这就不是2的次方;
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int m[10005];
#define mm 1000
int main()
{
int te;
cin >> te;
while (te--)
{
memset(m, 0, sizeof(m));
string n;
cin >> n;
int len = n.length();
for (int s = len - 1; s >= 0; s--)
{
m[s] = n[s]-'0';
}
int lenn;
int out[10005];
int sum=1, d = 0, k = 0;
while (sum)
{
sum = 0;
for (int s = 0; s < len; s++)
{
d = m[s] / 2;
sum += d;
if (s == len - 1)
{
out[k++] = m[s] % 2;
}
else
{
m[s + 1] += (m[s] % 2) * 10;
}
m[s] = d;
}
}
int spot = 1;
for (int s =0; s <k-1; s++)
{
if (out[s] != 0)
{
spot = 0;
break;
}
}
if (spot)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}