ACM simulation questions (1)-High Precision

Source: Internet
Author: User

Java provides byte, short, int, and long to represent integers, float and double to represent floating point numbers, each type has a certain range of representation, after this range is exceeded, it cannot be processed. To process very large integers and floating-point numbers, Java provides BigDecimal and BigInteger. The following code demonstrates the basic usage of BigDecimal and BigInteger:

BigDecimal data1 = new BigDecimal ("23232123456789.123456789 ");

BigDecimal data2 = new BigDecimal ("23423423123456789.123456789 ");

BigDecimal data3 = data1.add (data2 );

System. out. println (data3.toString ());

BigInteger iData1 = new bigint ("123123123456456789789123456789 ");

BigInteger iData2 = new bigint ("12312312323232456456789789123456789 ");

BigInteger iData3 = iData1.add (iData2 );

System. out. println (iData3.toString ());

If these class libraries are not used, how can we calculate large integers and large floating-point numbers? The following describes how to use ACM simulation questions.

1. Exponentiation (skip to the Chinese section below if you do not want to read English)
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. dont print the decimal point if the result is an integer.

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592 9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

. 00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201


The meaning of the question is to calculate the power of n (0 <n <= 25) of R (0.0 <R <99.999.

Question Analysis:

The key to this question is how to represent a large integer or floating point number. Calculate the N power for this question. The decimal position can be processed separately. If the R contains x decimal places, the result after calculation should include x * n decimal places. You can use arrays to represent R and then perform operations. The following method is for reference.

Public static String exponentiation (String data, int n ){

// Find the decimal point

Int index = data. indexOf (".");

// Remove the decimal point

String temp = data. replace (".","");

// Use an array to represent the input data

Byte [] inputData = new byte [temp. length ()];

// Indicates the calculation result.

Byte [] result = new byte [160];

// Indicates carry

Byte pre = 0;

// Initialization

For (int I = 0; I <inputData. length; I ++ ){

Result [I] = Byte. parseByte (temp. substring (temp. length ()-i-1, temp. length ()-I ));

InputData [I] = result [I];

}

// Calculate n-1 times

For (int I = 0; I <n-1; I ++ ){

// Indicates the last calculation result

Byte [] tempResult = Arrays. copyOf (result, result. length );

// Initialize the result

Arrays. fill (result, 0, result. length, (byte) 0 );

// Multiply the last calculation result by the input data

For (int k = 0; k <inputData. length; k ++ ){

For (int j = 0; j <150; j ++ ){

// Multiply two digits

Byte tempMul = (byte) (tempResult [j] * inputData [k] + result [j + k]);

// Process a single bit

Result [j + k] = (byte) (tempMul % 10 );

// Process carry

& N

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.