您的位置:首页 > 编程语言 > C语言/C++

leetcode_326_Power of Three(easy)(C++)

2016-03-24 12:57 471 查看
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;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode