3的幂

class Solution {
public:
    bool isPowerOfThree(int n) {
        if(n<=0)return false;
        const int maxint=0x7fffffff;
        int k=log(maxint)/log(3);
        int answer=pow(3,k);
        return (answer%n==0);
    
    }
};

 

1 class Solution {
2 public:
3     bool isPowerOfThree(int n) {
4    while(n&&n%3==0){
5        n/=3;
6    }
7     return n==1;
8     }
9 };

 

posted @ 2018-05-13 15:32  newmoonn  阅读(118)  评论(0)    收藏  举报