#include<iostream>
using namespace std;
bool isPower(int n, int a)
{
//思路很巧妙
while(0 == n % a)
n /= a;
if(1 == n)
return true;
return false;
}
//如果base是2,则可巧用位运算
bool is2Power(int n)
{
return !(n & (n - 1));
}
int main()
{
int i;
for(i = 1; i <= 1000; i++)
if(isPower(i, 3))
cout << i << " ";
cout << endl;
for(i = 1; i <= 1000; i++)
if(is2Power(i))
cout << i << " ";
cout << endl;
return 0;
}
判断n是否为a的x次方(n, a, x都为正整数)
本文转载:CSDN博客