[ACM] calculate the power of high precision and the power of acm.

Source: Internet
Author: User

[ACM] calculate the power of high precision and the power of acm.

Source: http://poj.org/problem? Id = 1001 & lang = zh-CN

Power of High Precision
Time Limit:500 MS   Memory Limit:10000 K
Total Submissions:160807   Accepted:39157

Description

It is a very common problem to perform high-precision calculation on numbers with large numbers and high precision. For example, calculating national debt is a problem of this type.

The problem you want to solve now is: for a real number R (0.0 <R <99.999), The Write Program is required to accurately calculate the n power (Rn) of R ), where n is an integer and 0 <n <= 25.

Input

T input includes multiple groups of R and n. The R value accounts for 1st to 6th columns, and the n value accounts for 8th and 9th columns.

Output

For each group of input, a row must be output, and the row contains the exact Nth Power of R. The output must be removed from the leading 0 and not 0. If the output is an integer, do not output the decimal point.

Sample Input

95.123 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

Bytes



Solution:
1. Use an array to store data, ensure that the array elements do not exceed 9, and do not store decimal points. Use an integer variable to record the digits of decimal places. Use string to accept the data of the input stream, and then convert it to an integer array (excluding "."), which is used as a factor for the first multiplication calculation and generates an integer as another factor.
2. The core function is void multiplicationCompute (vector <int> & ivecA, unsigned int & pointPosA, const int & intR, const int & pointPosR ), calculate the product of the big integer and base number stored in the array.
For easy calculation, big integers are stored in the high-to-low position of the array. During calculation, the array is traversed from the low position to the high position. Each array element (non-1 placeholder) is multiplied by the base number, and the result is saved to the array in sequence. For the product of ivecA [index] and base number, the single digit is saved to the index bit, and the higher part is saved to the index -- bit in turn. Function compute is called cyclically, and the final result is stored in ivecA.
3. Output Control. Integers do not output decimal places; decimal places suffixes 0 do not output; pure decimal places do not output 0 before the decimal point; if the real digits stored in ivecA are smaller than decimal places, add 0 before.


However, the following code cannot be submitted. The test results on the local machine are correct, but the submitted results are wrong answer. The error cause is not found at half past one. Let's take a look at it later.

1 # include <iostream> 2 # include <string> 3 # include <vector> 4 5 using namespace std; 6 7 8 // Element =-1? Reset 0! 9 inline void resetElement (int & ival) 10 {11 if (ival =-1) 12 {13 ival = 0; 14} 15} 16 17 // Element> 10? Deal element! 18 inline void dealElement (vector <int> & ivec, vector <int>: size_type index) 19 {20 while (ivec [index]> = 10) 21 {22 int temp = ivec [index]; 23 ivec [index] % = 10; 24 index --; 25 resetElement (ivec [index]); 26 ivec [index] + = (temp/10); 27} 28} 29 30 // ivecR [index] * intR 31 void multiplicationCompute (vector <int> & ivecA, unsigned int & pointPosA, const int & intR, const int & pointPosR) 32 {33 ve Ctor <int>: size_type currIndex = 0; 34 unsigned int val = 0; 35 36 for (vector <int >:: size_type index = 0; index! = IvecA. size (); ++ index) 37 {38 if (ivecA [index]! =-1) 39 {40 currIndex = index; 41 val = ivecA [index] * intR; 42 43 ivecA [index] = 0; // + = 44 45 while (val) 46 {47 resetElement (ivecA [currIndex]); 48 ivecA [currIndex] + = (val % 10); 49 dealElement (ivecA, currIndex); 50 val/= 10; 51 currIndex --; 52} 53} 54} 55 pointPosA = pointPosA + pointPosR; 56} 57 58 59 int main (int argc, char * argv []) 60 {61 62 string strR; 63 unsigned int n; 64 65 wh Ile (cin> strR> n) 66 {67 unsigned int intR = 0; 68 vector <int> ivecR; 69 vector <int> ivecA (200,-1 ); 70 unsigned int pointPositionR = 0; 71 unsigned int pointPositionA = 0; 72 73 74 // convert R to an int-type vector. pointPositionR records the decimal place (its value indicates the number of digits of the decimal place) 75 for (string: size_type index = 0; index! = StrR. size (); ++ index) 76 {77 if (strR [index]! = '. ') 78 {79 ivecR. push_back (strR [index]-'0'); 80} 81 else 82 {83 pointPositionR = strR. size ()-1-index; 84} 85} 86 87 // convert ivecR to intR 88 for (vector <int >:: size_type index = 0; index! = IvecR. size (); ++ index) 89 {90 intR = intR * 10 + ivecR [index]; 91} 92 93 94 // copy ivecR to ivecA, high-low-level storage, pointPositionA = pointPositionR 95 for (int indexA = ivecA. size ()-1, indexR = ivecR. size ()-1; indexA> = 0 & indexR> = 0; -- indexA, -- indexR) 96 {97 ivecA [indexA] = ivecR [indexR]; 98} 99 pointPositionA = pointPositionR; 100 101 // if (n = 0) 102 // {103 // cout <1 <endl; 104/return 0; 105 //} 106 107 while (n> = 2) // if n = 1, ivecA is the result 108 {109 multiplicationCompute (ivecA, pointPositionA, intR, pointPositionR ); 110 n --; 111} 112 113 114 // decimal point. If the decimal point is greater than ivecA, add 0115 for (vector <int >:: size_type index = ivecA. size ()-1; index> = ivecA. size ()-pointPositionA; -- index) 116 {117 if (ivecA [index] =-1) 118 {119 ivecA [index] = 0; 120} 121 122} 123 124 // processing 125 for (int index = ivecA. size ()- 1; index> = 0 & ivecA [index] = 0; -- index) 126 {127 ivecA [index] =-1; 128} 129 130 131 vector <int>:: size_type indexBegin = 0; 132 while (indexBegin! = IvecA. size () & ivecA [indexBegin] =-1) 133 {134 indexBegin ++; 135} 136 137 if (indexBegin = ivecA. size () 138 {139 cout <0 <endl; 140} 141 else142 {143 for (vector <int>: size_type index = indexBegin; index! = IvecA. size (); ++ index) 144 {145 if (ivecA [index]! =-1) 146 {147 if (index = ivecA. size ()-pointPositionA) 148 {149 cout <". "; 150} 151 cout <ivecA [index]; 152} 153} 154 cout <endl; 155} 156 return 0; 157}View Code

 

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.