#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;
}


本文转载:CSDN博客