Exponentiation
| Time Limit: 500MS | Memory Limit: 10000K | |
| Total Submissions: 134609 | Accepted: 32936 |
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.
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<iomanip>
#include<string>
using namespace std;
#define max 10000
int xn(char fnum[],int n);
int fmul(char s1[],char s2[]);
char sum[max];
char result[max];
int fmul(char s1[],char s2[])
{
int ls1,ls2;//ÓÃÓÚ´æ´¢³¤¶È£¬½øÎ»ÐÅÏ¢
int i=0,j,p,q,m;
int k=0,flag;
char mid[max];
ls1=strlen(s1);
ls2=strlen(s2);
//·´Ïò
j=ls1-1;
while(j>=0)
{
mid[i++]=s1[j--];
}
mid[i]=s1[ls1];
strcpy(s1,mid);
i=0;
j=ls2-1;
while(j>=0)
{
mid[i++]=s2[j--];
}
mid[i]=s2[ls2];
strcpy(s2,mid);
//¼ÆËã
for(i=0;i<max;i++)
{
sum[i]='0';
}
i=j=k=0;
flag=0;
p=0;q=0;
while(s2[i]!='\0')
{
flag=0;
m=q;
for(m=0;m<q;m++)
{
mid[m]='0';
}
p=m;
while(s1[j]!='\0')
{
k=(s1[j]-'0')*(s2[i]-'0');
mid[p]=(flag+k)%10+'0';
flag=(flag+k)/10;
j++;p++;
}
if(flag!=0){mid[p++]=flag+'0';}
mid[p]='\0';
flag=0;
p=0;
if(i==0)
{
while(mid[p]!='\0')
{
sum[p]=mid[p];
p++;
}
}
else
{
while(mid[p]!='\0')
{
k=(sum[p]-'0')+(mid[p]-'0');
sum[p]=(k+flag)%10+'0';
flag=(k+flag)/10;
p++;
}
if(flag!=0)
{
sum[p++]=flag+'0';
flag=0;
}
}
j=0;
i++;q++;
}
sum[p]='\0';
i=0;j=0;
strcpy(mid,sum);
p-=1;
while(p>=0)
{
sum[j]=mid[p];
p--;
j++;
}
sum[j]='\0';
return 0;
}
int xn(char fnum[],int n)
{
char s1[max];
int i;
if(n==0)
{
strcpy(result,"1");
return 0;
}
for(i=0;i<max;i++)
{
result[i]='\0';
}
strcpy(result,fnum);
for(i=1;i<n;i++)
{
strcpy(s1,fnum);
fmul(s1,result);
strcpy(result,sum);
}
return 0;
}
int main()
{
char s1[max],s2[6];
int i;
int n,ln,ls;
char ch;
int m,j=0,fl[100];
char s[6];
char res[100][max];
m=0;
for(i=0;i<100;i++)
fl[i]=0;
while(cin>>s2>>n)
{
j=0;
i=0;
while(s2[i]!='\0')
{
if(s2[i]=='.')
{
ln=strlen(s2)-1-i;
fl[m]=ln;
}
else
{
s[j++]=s2[i];
}
i++;
}
s[j]='\0';
ln=ln*n;
xn(s,n);
ls=strlen(result);
i=0;
j=0;
while(result[i]!='\0')
{
if(i==(ls-ln))
{
res[m][j++]='.';
res[m][j++]=result[i++];
}
else
{
res[m][j++]=result[i++];
}
}
res[m][j]='\0';
m++;
}
for(i=0;i<m;i++)
{
j=strlen(res[i])-1;
while(j!=0&&fl[i]!=0)
{
if(res[i][j]!='0')
{
if(res[i][j]=='.')
res[i][j]='\0';
else
res[i][j+1]='\0';
break;
}
j--;
}
j=0;
while(res[i][j]!='\0')
{
if(res[i][j]!='0')
{
while(res[i][j]!='\0')
{
cout<<res[i][j++];
}
cout<<endl;
break;
}
j++;
}
}
return 0;
}
总结:
大数的n次方处理,代码中采用字符串来防止溢出。
本文探讨了如何处理大数的乘方运算,并通过字符串操作实现高精度计算,适用于计算机科学领域的数值处理。
731

被折叠的 条评论
为什么被折叠?



