Exponentiation
Exponentiation |
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 .
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 and insignificant trailing zeros should be suppressed in the output.
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
Miguel A. Revilla
2000-02-09
==================================
R占据前六位,n占8 9位,求R^n,去掉前缀后缀0
先记录下小数点的位置,去掉小数点计算,求出幂之后再加上小数点,处理后缀0和补0
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int pro[555],lenpro;char s[11],str[11],ans[555];
void temp(char a[])
{
int len=strlen(a);
for(int i=0;i<len/2;i++)
swap(a[i],a[len-1-i]);
}
void mul(char a[],char b[])
{
memset(pro,0,sizeof(pro));
temp(a);
temp(b);
int lena=strlen(a);
int lenb=strlen(b);
int len;
for(int i=0;i<lena;i++)
{
for(int j=0;j<lenb;j++)
{
pro[i+j]+=((a[i]-'0')*(b[j]-'0'));
}
}
/*for(int i=0;i<lena+lenb-1;i++)
printf("%d ",pro[i]);
printf("\n");*/
if(lena>lenb) len=lena+lena;
else len=lenb+lenb;
for(int i=0;i<=len;i++)
{
pro[i+1]+=pro[i]/10;
pro[i]=pro[i]%10;
if(pro[i+1]>0) lenpro=i+2;
}
pro[lenpro]='\0';
/*for(int i=0;i<lenpro;i++)
{
cout<<pro[i];
}
cout<<endl;*/
for(int i=0;i<lenpro;i++)
{
ans[i]=pro[i]+'0';
}
temp(ans);
temp(b);
//cout<<ans<<endl;
}
int main()
{
int n;
while(~scanf("%s",str))
{
scanf("%d",&n);
memset(ans,0,sizeof(ans));
int len=strlen(str);
int point=0,j=0;
for(int i=0;i<len;i++)
{
if(str[i]=='.')
{
point=5-i;
}
else
{
ans[j++]=str[i];
}
}
ans[j]='\0';
strcpy(s,ans);
for(int i=1;i<n;i++)
{
//cout<<"ans="<<ans<<endl;
//cout<<"s="<<s<<endl;
mul(ans,s);
}
point*=n;
//cout<<point<<endl;
len=strlen(ans);
if(len>point)
{
for(int i=0;i<len;i++)
{
ans[len-i]=ans[len-i-1];
if(i==point)
{
ans[len-i]='.';
//cout<<i<<endl;
break;
}
}
for(int i=len;i>=len-point;i--)
{
if(ans[i]!='0'&&ans[i]!='.')
{
ans[i+1]='\0';
break;
}
if(ans[i]=='.')
{
ans[i]='\0';
break;
}
}
}
else if(len==point)
{
printf(".");
for(int i=len;i>=len-point;i--)
{
if(ans[i]!='0')
{
ans[i+1]='\0';
break;
}
}
}
else if(len<point)
{
printf(".");
for(int i=0;i<point-len;i++)
printf("0");
for(int i=len;i>=point-len;i--)
{
if(ans[i]!='0')
{
ans[i+1]='\0';
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}