Problem 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>
#include <cstring>
#include <cstdio>
using namespace std;
//計算a^n
string get_ans(int a,int n)
{
int b[150],tmp;
for(int i=0;i<150;++i)b[i]=0;
b[0]=1;
for(int i=0;i<n;++i)
{
tmp=0;
for(int j=0;j<150;++j)
{
tmp=(b[j]*a)+tmp;
b[j]=tmp%10;
tmp/=10;
}
}
bool flag = false;
string ans;
for(int i=149;i>=0;--i)
{
if(!flag&&b[i])flag=true;
if(flag)ans+=('0'+b[i]);
}
return ans;
}
int main()
{
int a,b,c,n,count,tmp;
char str[6];
while(cin>>str>>n)
{
count=strlen(str)-2;
b=0;c=0;
bool flag=false,Flag=false;
for(int i=0;i<strlen(str);++i)
{
if(str[i]=='.')flag=true;
else if(flag) c=10*c+(str[i]-'0');
else
{
b=10*b+(str[i]-'0');
if(b>0&&!Flag)Flag=true;
if(!Flag)count--;
}
}
if(c)
{
if(b>9)count--;
else if(!b)count++;
while(c%10==0)
{
count--;
c/=10;
}
}
tmp=1;
for(int i=0;i<count;++i)tmp*=10;
a=b*tmp+c;
count*=n;//總的小數位數
if(!b&&!c){cout<<0<<endl;continue;}
if(b&&!c){cout<<get_ans(b,n)<<endl;continue;}
if(!b&&c)
{
cout<<".";
string ans = get_ans(c,n);
tmp=count-ans.length();
while((tmp--)>0)cout<<0;
cout<<ans<<endl;
continue;
}
string ans = get_ans(a,n);
int len=ans.length();
tmp=len-count;
for(int i=0;i<len;++i)
{
if(i==tmp)cout<<".";
printf("%c",ans[i]);
}
cout<<endl;
}
return 0;
}