剑指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; } }
每天都做更好的自己,不纠结于输赢成败