Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
思路:
在一个double类型的数字上加0.5取整强制转换成int类型就是取下整或上整
class Solution {
public:
bool isPowerOfThree(int n) {
double a = log(n)/log(3);
int b = (int)(a + 0.5);
if(fabs(a-b) < 1e-10)
return true;
return false;
}
};