Exponentiation (Java large real number)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1063

exponentiation

Time limit:2000/500 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8076 Accepted Submission (s): 2279


Problem descriptionproblems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt are a taxing experience for many computer systems.

This problem requires so 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 a integer such that 0 < n <= 25.

Inputthe input would consist of a set of pairs of values for R and N. The R value would occupy columns 1 through 6, and the N value would be in columns 8 and 9.

Outputthe output would consist of one line for each line of input giving the exact value of r^n. Leading zeros should is Su Ppressed in the output. Insignificant trailing zeros must not being printed. Don ' t print the decimal point if the result was an integer.

Sample Input95.123 120.4321 205.1234 156.7592 998.999 101.0100 12

Sample Output548815620517731830194541.899025343415715973535967221869852721.00000005148554641076956121994511276767154838481760200 72635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.7641210216181 6443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
1 ImportJava.math.BigDecimal;2 ImportJava.util.Scanner;3 4 5  Public classMain {6      Public Static voidMain (string[] args) {7Scanner s =NewScanner (system.in);8      while(S.hasnext ()) {9BigDecimal B =s.nextbigdecimal ();TenBigDecimal ans = bigdecimal.valueof (1); One         intn =s.nextint (); A          while(n--> 0) -Ans =ans.multiply (b); -String string =Ans.striptrailingzeros (). toplainstring (). toString (); the         if(String.startswith ("0."))) -String = string.substring (1); - System.out.println (string); -     } + s.close (); -     } +}

1,striptrailingzeros(), returns a value that is less than this number of type BigDecimal, but minus 0 of the tail.
2,toplainstring(), returns a string of type BigDecimal.
3,StartsWith(), determines whether the beginning of this instance matches the specified string.
4,substring(), returns a new string that is a substring of this string. The substring starts at the specified index and continues to the end of the string.

We all know that Java class is generally to take the method of ToString, BigDecimal there are tostring,toplainstring and toengineeringstring three kinds of methods to represent the string,

Here are the features of each of the three different methods:

tostring:using scientific notation If an exponent is needed;

Toengineeringstring:using Engineering notation If an exponent is needed.

Toplainstring:without an exponent field.

Exponentiation (Java large real number)

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.