cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

326.Power of Three

Posted on 2016-01-21 14:35  cKK  阅读(132)  评论(0编辑  收藏  举报
/*

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?*/
public class PowerOfThree {
    private  static final double EX=10e-15;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    int d=243;
        System.out.println(isPowerOfThree(d));
    }
    
     public static boolean isPowerOfThree(int n) {
            if(n==0)
                return false;
            double res=Math.log(n)/Math.log(3);
            System.out.println(res);
            return (Math.abs((res-Math.round(res)))<EX);  //注意为什么不能用floor和ceil   注意double   其实还可以bigdecimal?
        }
    }