这里必须是folat,如果是int,12到最后就是1。
flaot 12最后是1.5。
#include <iostream>
using namespace std;
int main() {
float n;
cin >> n;
while (n > 1.0) {
n /= 2;
}
if (n == 1.0) {
cout << "YES";
}
else {
cout << "NO";
}
return 0;
}
按位运算符
2次幂的数字 2进制的数字永远是以1开头,后边都是0。
2 ......10
1 ...... 1
4 ......100
3 ......011
8 ......1000
那么就可以(n&n-1)==0,就确定数是2的次方。
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if ((n & (n - 1)) == 0) {
cout << "YES";
}
else {
cout << "NO";
}
return 0;
}