Description
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
Input
T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output
对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
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 <math.h>
using namespace std;
#define M 10000
void faccib(char *s,char* sum,int n, int &leng)
{
char *p = s+strlen(s);
for(int i=1; i<strlen(s); i++)
if(*(p-i) == '.')
leng = i;
if(leng > 0)
{
char *pp = s;
while(*pp != '.') pp++;
char *ppp = pp+1;
while(*ppp != '\0')
*pp++ = *ppp++;
*pp = '\0';
}
int length = strlen(s);
char *temp2 = strdup(s);
temp2 = temp2+strlen(s)-1;
char *temp = strdup(s);
temp = temp+strlen(s)-1;
int sumlength = 0,sumleng2 = 0;
for(int i=0; i<length; i++)
sum[i] = *(temp-i);
int sumleng3 = strlen(s);
sumleng2 = sumleng3;
for(int i=1; i<n; i++)
{
char *temsum = strdup(sum);
memset(sum, '0', M);
for(int j=0,l=0; j<length; j++,l++)
{
sumlength = l;
for(int k=0; k<sumleng2; k++)
{
int sumint = (*(temp-j)-'0') * (*(temsum+k)-'0');
int ll = (sumint + (sum[sumlength]-'0'));
sum[sumlength] = (ll%10) + '0';
sum[sumlength+1] += ll/10;// + '0';
int othernum = 0;
if((othernum = (sum[sumlength+1]-'0'))>=10)
{
sum[sumlength+1] = othernum%10 + '0';
sum[sumlength+2] += othernum/10;
}
++sumlength;//++;
}
}
free(temsum);
char *pq = sum+99;
while(*pq == '0')
pq--;
*(pq+1) = '\0';
sumleng2 = strlen(sum);//sumleng3;
}
char *pq = sum+M-1;
while(*pq == '0')
pq--;
*(pq+1) = '\0';
free(temp);
free(temp2);
}
int main()
{
char s[10], sum[M]={'0'};
memset(s, '0', 10);
memset(sum, '0', M);
//cout<<sum<<endl;
int n,len = 0;
while(cin>>s>>n)
{
for(int i=strlen(s)-1; i>0; i--)
{
if(s[i] != '0')
{
s[i+1] = '\0';
break;
}
}
cout<<s<<endl;
faccib(s, sum, n, len);
//cout<<strlen(sum)<<" "<<len-1<<endl;
char *ss = sum+strlen(sum)-1;
if(len >0 )
{
int length = strlen(sum)+1;
if(length < n*(len-1))
{
cout<<".";
while(n*(len-1)-length>=0)
{
cout<<"0";
length++;
}
}
for(int i=0; i<strlen(sum)+1; i++)
{
if(i == strlen(sum)-n*(len-1))
cout<<".";
cout<<*(ss-i);
}
}else
{
for(int i=0; i<strlen(sum)+1; i++)
{
cout<<*(ss-i);
}
}
}
return 0;
}