http://poj.org/problem?id=1001
方法:
大整数乘法
这题没什么好说的,直接用字符串模拟整个过程
Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many 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 should 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
#include <iostream>
#include <string>
3:
namespace std ;
5:
//大浮点数相乘,此时系统提供的精度远远不能达到要求
7:
const string& rhs)
9: {
12:
//lengths of the two operators , including dot
14: size_t len1 = lhs.size() ;
15: size_t len2 = rhs.size() ;
int index=0 ;
17:
//carefully judge whether exist 0
//.000 or 0.000 or 000.
for( index=0 ; index<len1 ; ++index)
21: {
'.' )
break;
24: }
if( index==len1 )
;
27:
for( index=0 ; index<len2 ; ++index)
29: {
'.' )
break;
32: }
if( index==len2 )
;
35:
//copy the two operators , because later may delete their dots
37: string s1(lhs);
38: string s2(rhs);
//max length of the result
40: size_t lenResult = len1+len2 ;
41:
int val1,val2,tmpval,mul_carry,sum_carry ;
int i,j,k,t,m ;
44:
//initialize the result to 0
'0') ;
47:
48: size_t dotnum1,dotnum2,dotnumResult;
//find the positions of the dots in the two operators
) ;
) ;
52: string::size_type dotpos ;
53:
//there is no dot
55: dotnum1 = 0 ;
//the operator is float
57: {
//delete the dot , 12.34-->1234
//number of the digit behind the dot
//now the length decrease 1
61: }
62:
if( pos2 == string::npos )
64: dotnum2 = 0 ;
else
66: {
67: s2.erase( pos2 , 1 ) ;
68: dotnum2 = len2-pos2-1 ;
69: --len2 ;
70: }
71:
//number of digit behind the dot in the result
//the position of the dot in the result
74:
//s1*s2
76: {
'0') ;
//carry of multiply in calculating the tmpresult
//carry of sum in calculating the final result
//get the digit of s2 in reverse order to simulate the process of multiply
81:
for( j=len1-1 , t=k ; j>=0 ; --j , --t )
83: {
'0' ;
85: tmpval = val1*val2 + mul_carry ;
86: mul_carry = tmpval/10 ;
'0' ;
88: }
//put the final carry to the tmpresult
90:
//update the result
92: {
'0') + sum_carry ;
94: sum_carry = tmpval/10 ;
'0' ;
96: }
97: }
98: string::size_type resultBeginPos ;
99: string::size_type resultEndPos ;
//there is no dot
101: {
'0') ;
return result.substr(resultBeginPos); ;
104: }
else
106: {
//insert the dot into result
108:
'0') ;
'0') ;
//0.123000
return result.substr( resultBeginPos , resultEndPos-resultBeginPos+1 ) ;
//12.3400 or 12340.0000
114: {
//12.3400
return result.substr( resultBeginPos , resultEndPos-resultBeginPos+1 ) ;
//12340.0000
return result.substr( resultBeginPos , resultEndPos-resultBeginPos );
119: }
120: }
121: }
122:
//divide and conquer
int n)
125: {
if(n==0)
;
)
;
130:
if(n==1)
) ;
133: string tmpresult = power_bignum(R,n/2);
if( n%2 != 0 )
return multiply_bignum( R , multiply_bignum(tmpresult,tmpresult) );
else
return multiply_bignum(tmpresult,tmpresult) ;
138: }
139:
void run1001()
141: {
142: string r;
int n;
while(cin>>r>>n)
145: cout<<power_bignum(r,n)<<endl;
146: }