zoukankan      html  css  js  c++  java
  • 剑指offer_面试题11 数值的整数次方_考察代码的完整性

    测试通过代码:

    package t0825;
    
    public class Power {
         public static void main(String[] args){
            System.out.println(Power(2.5,3));
            System.out.println(Power(0.00000001,3));
            System.out.println(Power(0.00000001,-3));
            System.out.println(Power(2,-3));
            System.out.println(Power(10,10));         
         }
         /*
          * 大数问题
          */
         public static double Power(double base, int exponent) {
             double result=1.0;
             if(equal(base,0.0) && exponent<0){ //当基于为零时,多少次方都为零
                return 0.0; 
             }    
             int absExponent=0;
             if(exponent<0)
                 absExponent=-exponent;   //如果为负数,取反求倒数;
             else
                 absExponent=exponent;
             result = PowerAlthmn(base,absExponent);
             if(exponent<0)
                 result = 1.0/result;         
             return result;
         }
         public static boolean equal(double num1,double num2){
            if((num1-num2)>-0.0000001 && (num1-num2<0.0000001))
                return true;
            else
                return false;
         }
         public static double PowerAlthmn(double base,int exponent){
             double result1=1.0;
             while(exponent!=0){
                 result1=result1*base;
                 exponent--;
             }         
             return result1;
         }
    }
  • 相关阅读:
    二叉树
    bfs
    E-Gold Coins
    D-We Love MOE Girls
    A
    哈希--查找出现过的数字
    二分查找
    KMP简单应用
    KMP算法(2)
    [JSOI2008]最大数
  • 原文地址:https://www.cnblogs.com/snowwhite/p/4758649.html
Copyright © 2011-2022 走看看