For various reasons, I have been in poor mood for a while recently. Write a few questions to entertain me.
I learned some JAVA and wrote some things such as pushing boxes last winter, but I have never used JAVA, I have long heard that JAVA uses big numbers directly for large numbers and high-precision means. Today, I have come to the water to have some fun.
POJ1001
Description
Problems involving the computation of exact values of very large magnstrap and precision are common. For example, the computation of the national debt is a taxing experience for your 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.
Input
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.
Output
The output will consist of one line for each line of input giving the exact value of R ^ n. leading zeros shocould be suppressed in the output. insignificant trailing zeros must not be printed. don't print the decimal point if the result is an integer.
Sample Input
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
Code:
import java.util.*;import java.math.*;import java.io.*;public class poj1001 {public static void main(String args[]){Scanner cin = new Scanner(System.in);while(cin.hasNext()){String a = cin.next();int t = cin.nextInt();BigDecimal ans = new BigDecimal(a);ans = ans.pow(t);String result = ans.stripTrailingZeros().toPlainString();if(result.charAt(0)=='0') result=result.substring(1);System.out.println(result);}}}
The math package contains two classes for processing large numbers: BigInteger and BigDecimal. As the name suggests, one class is used to process integers and the other class is used to process rational numbers;
Common calculation methods such as add (), subtract (), pow (), and abs () can be used directly.
BigDecimal has several more formatting methods, which is useful for processing outputs of different formats.
StripTraillingZeros (): removes all values that do not affect the value size;
1.50-> 1.5;
1.00-> 1;
This function is useful.
We all know that JAVA classes generally use the toString method, while BigDecimal has three methods to express toString, toPlainString, and toEngineeringString as strings,
The features of these three methods are as follows:
ToString: using
Scientific notationIf an exponent is needed;
ToEngineeringString: using
Engineering notationIf an exponent is needed.
ToPlainString:Without an exponent field.