ACM 1001 Exponentiation high-precision power floating point calculation

Source: Internet
Author: User

I haven't posted a post for a long time. Now I want to write down my efforts last night and this morning.

I copied the description of the question directly:


Exponentiation
Time Limit: 500 MS Memory Limit: 10000 K
Total Submissions: 121122 Accepted: 29574


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 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

I think the most difficult part of this question is that no data type can accommodate the last dozens of computation results. At this time, you may blurt out "using arrays is not OK ". Well, this is a good idea, but when you use an array to accommodate the final result, do you feel that the result came from. How to calculate a value of 95.123 ^ 12 to get the result.

 


My thoughts are as follows:

The operation is performed in the array, so how to calculate it in the array. When I was a child, I used a pen to calculate the number and multiply the number. That is, if 35*35 is a calculation process?

 


 

That is, the determination of one digit = the low position of the two numbers plus the carry of the low position

We are doing the following work to multiply two single digits: 1. Multiply + low

2. Carry to high places. If not, carry to 0.


3. Finally, the result of determining a digit is the number after 10 is obtained.

 

 

 

Then we can look at the final code:

 

For (I = 0; I <n; I ++) {for (f = 0, j = 0; j <= d | f> 0; j ++) {x = a [j] * B + f; f = x/10; a [j] = x % 10;} while (a [j] = 0) j --; // remove the leading 0 d = j; // d indicates the number of resets in the array}

Array a [] stores the final result, that is, 1225 after 35*35. the removal of leading zero is because the array is initialized to 0 before the operation, and a d is used to record the number of Reset values in the array, which is actually the number of digits in the final result, for example, in 35*35, d is equal to 4;

 


It must be noted that the operations in the array are all unit integer data, that is, when the number of input is float, you must extract the numbers one by one and put them into the array to be operated.

 

# Include <iostream> # include <string>/* defines the maximum number of input values. */# define MAX 10 using std: cin; using std: cout; using std: endl;/* Get the decimal digits. For example, if 1.234 is returned, the function returns 3 */int GetNumForDigit (char * str) {int I; int flag = 0; char * original = str; int digit; for (I = 0; I <MAX; I ++) {if (* str! = '? ') Str ++; else {flag = I; break;}/* because the string ends with' \ 0, therefore, the number of digits obtained at the end is larger than the original digit */flag --; str = original; for (I = 0; I <MAX; I ++) {if (* str! = '. ') Str ++; else {digit = I; break ;}} digit ++; // begin/* The operation is performed here to handle the case where the backend is invalid, for example, 1.0100, 2.4500000 */int num = 0; str = original; while (* str! = '? ') {Num ++; str ++;} num --; str = original; int count = 0; str + = num; str --; for (int I = 0; I <MAX; I ++) {if (* str = '0') {count ++;} else {break;} str --;} // endreturn flag-digit-count;}/* number of characters to be converted. For example, 1.23 is converted to 123. In special cases, 1.0100 is converted to 101, remove the following invalid zero */int CharTransformInt (char * str) {char * original = str; int num = 0; int result = 0; char temp = NULL; while (* str! = '? ') {Num ++; str ++;} num-= 2;/* begin remove the backend invalid zero */str = original; bool flag = false; for (int I = 0; I <MAX; I ++) {if (* str = '. ') {flag = true; break;} str ++;} str = original; if (flag) {str + = num; for (int I = 0; I <MAX; I ++) {if (* str = '0') {num --;} else {break;} str -- ;}// endint num_size = 1; while (-- num! = 0) {num_size * = 10;} str = original; for (int I = 0; I <MAX; I ++) {if (* str! = '.' & * Str! = '? ') {Temp = * str;/* the method in which the atoi parameter ends */result + = atoi (& temp) * num_size; num_size/= 10;} str ++;} return result;} int main () {int digit = 0, digits = 0, I; char str [MAX]; int n; int a [1000]; for (I = 0; I <MAX; I ++) {str [I] = '? ';} For (I = 0; I <1000; I ++) // set all arrays to 0 {a [I] = 0 ;} while (cin> str> n) {digit = GetNumForDigit (str);/* obtain the decimal digits */for (I = 0; I <n; I ++) // digits is the number of decimal places after multiplication {digits + = digit;} int B = CharTransformInt (str); int d = 0; a [0] = 1; int j, k, x, f;/* place the number in the array for calculation. The algorithm is the process of multiplying two numbers in the pen calculation */for (I = 0; I <n; I ++) {for (f = 0, j = 0; j <= d | f> 0; j ++) {x = a [j] * B + f; f = x/10; a [j] = x % 10;} while (a [j] = 0) j --;/ /Remove the leading 0 d = j; // d indicates the number of Reset items in the array}/* The result here is when m <1 */if (d <(digits-1 )) {cout <'. '; for (k = 0; k <(digits-1)-d; k ++) cout <0 ;} /* The processing condition here is when m> 1 */for (I = d; I> = 0; I --) {if (I = (digits-1) cout <'. '; cout <a [I] ;}cout <endl; // begin resets data to prepare for the next cycle (I = 0; I <MAX; I ++) {str [I] = '? ';} For (I = 0; I <1000; I ++) // set all arrays to 0 {a [I] = 0;} digit = 0; digits = 0; // end} return 0 ;}

 

Related Article

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.