求x的y次幂

public class Main {

    public static void main(String[] args) {
        System.out.println(power(3,18));
    }

    //求x的y次幂
    private static long power(long x, long y){
        if ( y == 1)
            return x ;
        else
        {
            if (y % 2 == 1)
                return x * power(x*x,y/2);
            else
                return power(x*x,y/2);
        }
    }
}

posted on 2010-07-06 21:57  sunliho  阅读(254)  评论(0编辑  收藏  举报