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?
class Solution {
public:
bool isPowerOfThree(int n) {
return (fmod(log10(n)/log10(3),1))==0;
}
};
记录一下函数 fmod 的用法如下:
原型:extern float fmod( float x, float y);
用法:#include < math.h >
功能:计算x/y的余数
说明:返回 x-n*y,符号同 x 。n=[x/y] (向靠近零的方向取整)
例如:fmod(3.6 , 1)= 0.6; fmod(-3.6 , 1)= -0.6;fmod(3.6 , -1)= 0.6