您的位置:首页 > 其它

[326]Power of Three

2016-08-27 20:28 106 查看
【题目描述】

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?
【题目思路】
如果是3的幂必然取以3为底的对数得到的是整数,而stl库里有log的函数,因此只要判断是否以3为底的对数是否为整数即可

【代码】

class Solution {
public:
bool isPowerOfThree(int n) {
double ans=log10(n)/log10(3);
return (ans-int(ans))==0?true:false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: