Sword refers to Java Implementation of offer programming questions-the integer power of the value of interview question 11, and sword refers to offer

Source: Internet
Author: User

Sword refers to Java Implementation of offer programming questions-the integer power of the value of interview question 11, and sword refers to offer


Question:

Implement the double power (double base, int exponent) function to calculate the power of base's exponent. Do not use library functions, and do not need to consider large numbers.

Solution: the most common way to realize the n power of a value is to concatenate a number itself n times.
The base number must take into account positive numbers, negative numbers, and zero.
The positive integer, negative integer, and zero must be taken into account for the exponent. There are nine possible cases, especially when the base number is 0 and the index is negative, it is meaningless.
If the exponent is negative, you can first obtain the positive value based on the exponent, and then calculate the reciprocal to obtain the real result.
Solution 1: comprehensive and inefficient, taking into account all boundary conditions and Negative tests, using the cyclic computing multiplication method
Solution 2: a comprehensive, efficient and perfect algorithm that uses formulas and recursion to reduce the number of computations

A ^ n = a ^ (n/2) * a ^ (n/2) n is an even number.

A ^ n =A ^ (n/2) * a ^ (n/2) & a n is an odd number

 

1 package Solution; 2/** 3 * offoffoffoffer interview question 11: The integer power of the value 4 * question: implement the function double power (double base, int exponent ), evaluate the base's exponent power. 5 * you are not allowed to use database functions, and do not need to consider the large number issue. 6 * solution: to realize the Npower of a value is to concatenate a number itself for n times. 7 x base numbers must take into account positive numbers, negative numbers, and zero. 8 x indexes must take into account positive integers, negative integer and zero. There are nine possible cases. In particular, it is meaningless to note that the base number is 0 and the index is negative. Therefore, we need to perform multiplication operations with 9 * negative indexes, you can first obtain the result based on the positive exponent and then calculate the reciprocal. 10 * solution 1: comprehensive and inefficient. Considering all the boundary conditions and Negative tests 11 * solution 2: comprehensive, efficient, and perfect algorithms, use the formula a ^ n = a ^ (n/2) * a ^ (n/2). n is an even number 12 * a ^ (n/2) * a ^ (n/2) & a n is an odd number 13 * @ author GL14 * 15 */16 public class No11Power {17 18 public static void main (String [] args) {19 System. out. println ("base number is 2, exponent is 2, and the result is:" + power (); 20 System. out. println ("base number is 2, exponent is-2, and the result is:" + power (2,-2); 21 System. Out. println ("the base number is 2, and the exponent is 0. The result is:" + power (); 22 System. out. println ("base number is-2, exponent is 2, calculation result is:" + power (-); 23 System. out. println ("base number is-2, exponent is-2, calculation result is:" + power (-2,-2); 24 System. out. println ("base number is-2, exponent is 0, the result is:" + power (-); 25 System. out. println ("the base number is 0, and the exponent is 2. The calculation result is:" + power (); 26 System. out. println ("the base number is 0, and the exponent is 0. The result is:" + power (); 27 System. out. println ("base number is 2, exponent is 1, and the result is:" + power (); 28 System. out. println ("the base number is 0, and the exponent is-2.: "+ Power (0,-2); 29} 30 31 public static double power (double base, int exponent) {32 if (equal (base, 0.0) & exponent <0) 33 throw new RuntimeException ("while exponent is minus, the base can't be zero"); 34 int absExponent = exponent; 35 if (exponent <0) 36 absExponent = ~ Exponent + 1; // returns the reverse Number of integers plus 1 by bit. The opposite number is 37 // double result = powerWithUnsignedExponent (base, absExponent); 38 double result = powerWithUnsignedExponentByRecursion (base, absExponent); 39 if (exponent <0) 40 result = 1.0/result; 41 return result; 42} 43 // solution 1: Perform the multiplication operation when the exponent is not negative, concatenation 44 private static double powerWithUnsignedExponent (double base, int absExponent) {45 double result = 1.0; 46 for (int I = 1; I <= absExponent; I ++) {47 result = result * base; 48} 49 return result; 50} 51 52 // solution 2: half the exponent is calculated when a number is multiplied, the obtained result can be obtained by multiplying, which greatly reduces the calculation workload by 53 private static double powerWithUnsignedExponentByRecursion (double base, int exponent) {54 if (exponent = 0) 55 return 1.0; 56 if (exponent = 1) 57 return base; 58 double result = powerWithUnsignedExponentByRecursion (base, exponent> 1); 59 result = result * result; 60 if (exponent & 0x1) = 1) 61 result = result * base; 62 return result; 63} 64 65 66 // you cannot use = to determine whether or not to wait for a floating point number due to precision issues. If the two numbers meet certain conditions, they can be considered as 67 private static boolean equal (double number1, double number2) {68 if (number1-number2>-0.0000001 & (number1-number2) <0.0000001) 69 return true; 70 return false; 71} 72}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.