Exponentiation
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
高精度乘法,输出的时候注意格式的处理
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
long n;
long a[400], b[400];
long temp;
void cheng(long *a, long *b)
{
long tmp[400];
memset(tmp, 0, sizeof(tmp));
for (long i = 1; i <= a[0]; i++)
for (long j = 1; j <= b[0]; j++)
{
tmp[i + j -1] += a[i] * b[j];
tmp[i + j] += tmp[i + j -1] / 10;
tmp[i + j -1] %= 10;
}
long len = a[0] + b[0];
while (tmp[ len ] == 0) len--;
if (len >= 200) { len = 200; tmp[200] = 9; }
tmp[0] = len;
for (long i = 0; i <= tmp[0]; i++)
{
a[i] = tmp[i];
}
}
void outit()
{
//cout << a[0] << ' ' << temp <<endl;
if (a[0] <= temp)
{
printf(".");
for (long i = 1; i <= temp - a[0]; i++)
{
printf("0");
}
long r = 1;
while (a[r] == 0)
{
r++;
}
for (long i = a[0]; i >= r; i--)
{
printf("%d", a[i]);
}
printf("\n");
}
else {
long r = 1;
while (a[r] == 0)
{
r++;
}
//cout << r<<endl;
for (long i = a[0]; i >= r; i--)
{
if (i == temp) printf(".");
printf("%d", a[i]);
}
if (temp+1 < r)
{
for (long i = r; i>temp+1; i--)
{
printf("0", a[i]);
}
}
printf("\n");
}
}
int main()
{
freopen("A.in", "r", stdin);
while(cin>>s>>n)
{
long l = s.length();
long t = 1;
long k = 0;
temp = 0;
for (long i = l - 1; i >= 0; i--)
{
if (s[i] != '.')
{
b[t] = s[i] - '0';
t++;
}
else k = t-1;
}
//cout <<k<<endl;
b[0] = t;
a[1] = 1;
a[0] = 1;
for (long i = 1; i <= n; i++)
{
temp +=k;
cheng(a, b);
}
outit();
}
return 0 ;
}