Question (in Chinese): http://ac.jobdu.com/problem.php?pid=1514
Question Name: Power
Question Description: Give a double and an integer, without using the math library, compute the base to the power of exponent.
Input: may contains multiple test cases. The first line indicates how many test cases are there in the following input. Each of the next lines contains a double and then an integer (separated with a space).
Output: the power result for each test case.
1 2 3 4 5 6 7 8 9 10 11 12 13 | Input: 5 1.0 10 0.0 -5 1.0 0 1.2 5 2.0 -1 Output: 1.00e+00f INF 1.00e+00f 2.49e+00f 5.00e-01f |
I used half hour to solve the power, and then two hours for the output issue. If anyone know why, please do leave a comment!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /** * Filename : Power.java * Dependency: None * Author : Sheng Yu (codesays.com) * Create at : 2014-08-05 * JDK used : 1.7 * Changelog : None */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.text.DecimalFormat; public class Power { /** * To compute the base to the power of exponent. * @param base : double, the base of the power computation. * @param exponent : int, the exponent of the power computation. * @return double : base ** exponent. */ public double power(double base, int exponent) { // Handle the special case that base is 0.0 if (Power.doubleEqual(base, 0.0)) { if (exponent == 0) return 1.0; else if (exponent > 0) return 0.0; else throw new ArithmeticException("Divided by Zeor."); } if (exponent == 0) return 1.0; // Remember that exponent might be negative. boolean negative = false; if (exponent < 0.0) { negative = true; exponent = -exponent; } double result = 1.0; result = power(base, exponent >> 1); result = result * result; // Exponent is odd. if ((exponent & 1) == 1) result *= base; if (negative) return 1.0 / result; else return result; } /** * Check whether two double numbers are equal. * @param first : the first double number * @param second : the second double number * @return boolean: whether the input numbers are eauql. */ static public boolean doubleEqual(double first, double second) { double error = 1E-7; if (Math.abs(first - second) < error) return true; else return false; } /** * Stub for testing. */ public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); st.nextToken(); int testCount = (int) st.nval; double base = 0.0; int exponent = 0; Power calculator = new Power(); Double result = 0.0; DecimalFormat resFormat = new DecimalFormat("0.00E00fn"); String strRes = null; StringBuffer sb=new StringBuffer(); while (testCount > 0 && st.nextToken() != StreamTokenizer.TT_EOF) { base = st.nval; st.nextToken(); exponent = (int) st.nval; try { result = calculator.power(base, exponent); } catch (ArithmeticException e) { result = Double.NaN; } if (result.isNaN()) { sb.append("INFn"); } else { strRes = resFormat.format(result); if (strRes.contains("E-")) strRes = strRes.replace("E", "e"); else strRes = strRes.replace("E", "e+"); sb.append(strRes); //System.out.println(String.format("%3.2ef",result)); } testCount -= 1; } System.out.print(sb); } } |