Exponentiationjava
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 173641 | Accepted: 41958 |
Descriptionui
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.code
Inputip
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.ci
Outputinput
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.string
Sample Inputit
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
Sample Outputio
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
題目大意:求m^n,保證精度以及考慮小數的特殊輸出。table
注意: 題目不難。但考慮的狀況不能漏。請確保以下輸入和輸出
95123.0 12 548815620517731830194541899025343415715973535967221869852721 95123 12 548815620517731830194541899025343415715973535967221869852721 95.123 12 548815620517731830194541.899025343415715973535967221869852721 0.95123 12 .548815620517731830194541899025343415715973535967221869852721 0.951230 12 .548815620517731830194541899025343415715973535967221869852721 951230 12 548815620517731830194541899025343415715973535967221869852721000000000000
Solution: 代碼比較長,基本是別人的5倍。。m^n主要是利用BigDecimal當作整數計算,而後再考慮小數點的位置。
import java.io.BufferedReader; import java.io.InputStreamReader; import java.math.BigDecimal; public class Main { public static String dot(String res, String s, int n) { //給得出的臨時結果添加小數點和整數 s = s.substring(s.indexOf(".") + 1); char[] cArr = s.toCharArray(); int end = cArr.length - 1; for (int i = cArr.length - 1; i >= 1; i--) { //小數最末尾的0無效,不影響結果精度 if (cArr[i] == '0') { end--; } else { break; } } String result = ""; int count = (end + 1) * n; //小數點後數的個數 if (res.length() <= count) { int n0 = count - res.length(); while (n0-- > 0) { result += "0"; } result = "." + result + res; } else { result = res.substring(0, res.length() - count) + "." + res.substring(res.length() - count); } return result; } public static String calculate(BigDecimal b, int n) { // 先按整數計算得出臨時結果 BigDecimal result = new BigDecimal(1); for (int i = 0; i < n; i++) { result = result.multiply(b); } return result.toString(); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while ((line = br.readLine()) != null) { String[] s = line.split("\\s+"); int n = Integer.parseInt(s[1]); if (s[0].indexOf(".") == -1) { // 針對95123 12這種狀況 System.out.println(calculate(new BigDecimal(Integer.parseInt(s[0])), n)); } else if(Integer.parseInt(s[0].substring(s[0].indexOf(".") + 1)) == 0) { // 針對95123.0 12 System.out.println(calculate(new BigDecimal(Integer.parseInt(s[0].substring(0, s[0].indexOf(".")))), n)); } else { // 用於去掉相似0.951230末尾的0這種狀況 String t = s[0].replace(".", ""); int end = t.length(); for (int i = end - 1; i >= 0; i--) { if (t.charAt(i) == '0') { end--; } else { break; } } t = t.substring(0, end); System.out.println(dot(calculate(new BigDecimal(Integer.parseInt(t)), n), s[0], n)); } } br.close(); } }
最終結果:
固然,也能夠不用BigDecimal, 利用原始加減乘除的計算方式來手動計算臨時的整數結果