原题:
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?
AC后的代码(注意,该代码只对素数有用):
class Solution {
public:
bool isPowerOfThree(int n) {
//some int =3^k;
//k=log3(some max int)
if(n<=0) return false;
const int maxint =0x7fffffff;
int k=log(maxint)/log(3);
int somgbig3=pow(3,k);
return (somgbig3%n==0);
}
};
本文介绍了一个用于确定给定整数是否为3的幂的算法,并提供了一段C++实现代码作为示例。文章探讨了如何通过数学运算避免使用循环或递归的方法来解决问题。
808

被折叠的 条评论
为什么被折叠?



