#大数乘法
#问题
Exponentiation
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 179466 | Accepted: 43261 |
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 R n 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 R n 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
#AC码
#include<iostream>
#include<cstring>
#include<string.h>
using namespace std;
int main()
{
string r;
int n,dianwei;
const int R_LEN=160;
short result[R_LEN],temp[R_LEN],num[6];
//freopen("1001.txt","r",stdin);
while(cin>>r>>n)
{
int len=0;
memset(result,0,sizeof(result));
memset(temp,0,sizeof(temp));
memset(num,0,sizeof(num));
dianwei = 0;
size_t pos = r.find(".");
if(pos != string::npos) //找到'.'
dianwei=(5-pos)*n;
for(int i=5,j=0;i>=0;i--)
{
if(r[i]!='.')
{
temp[j]=result[j]=num[j]=r[i]-'0';//提取数位
++j;
}
}
while(n>=2)//指数大于等于2的时候计算,否则就是直接去除多余0输出了
{
memset(result,0,sizeof(result));
for(int i=0;i<5;i++)//乘数数位循环
{
//被乘数数位循环
for(int j=0;j<R_LEN;j++)
{
if(num[i]==0)
break;
result[i+j]+=num[i]*temp[j];
//处理进位
for(int t=i+j;result[t]>=10;t++)
{
result[t+1]+=result[t]/10;
result[t]=result[t]%10;
}
}
}
for(int i=0;i<R_LEN;++i)
temp[i]=result[i];//当前结果记录,还剩n-2次幂没有求
n--;
}
int firstindex=-1;
for(int i=R_LEN-1;i>=dianwei;i--)//查找小数点前第一个不为0的数(小数点前输出起点)
{
if(result[i]>0)
{
firstindex=i;
break;
}
}
int lastindex=-1;
for(int i=0;i<dianwei;i++)//查找小数点后最后一个不为0的数(小数点后输出终点)
{
if(result[i]>0)
{
lastindex=i;
break;
}
}
if(firstindex!=-1)//输出小数点前的部分
{
while(firstindex>=dianwei)
{
cout<<result[firstindex];
firstindex--;
}
}
if(lastindex!=-1)//如果有小数部分,先输出小数点,再输出小数部分
{
cout<<'.';
dianwei--;
while(dianwei>=lastindex)
{
cout<<result[dianwei];
dianwei--;
}
}
cout<<endl;
}
}