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
练习一下高精度乘法。
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define Max_Size 128
char buf1[20], buf2[20];
int res[Max_Size];
void calc(int n, int m, int cur)
{
int i, j;
memset(res, 0, sizeof(res));
res[0] = 1;
for (i = 1; i <= n; i++)
{
int v = 0;
for (j = 0; j < Max_Size; j++)
{
int temp = res[j] * m + v;
res[j] = temp % 10;
v = temp / 10;
}
}
int k =(int)(n * log10(m)) + 1;
int count = 0;
if (cur > k)
{
printf(".");
for (j = 1; j <= cur - k; j++)
printf("0");
j = Max_Size - 1;
while (!res[j])
j--;
for (; j >= 0; j--)
printf("%d", res[j]);
}
else
{
j = Max_Size - 1;
while (!res[j])
j--;
for (; j >= 0; j--)
{
if (count == k-cur)
printf(".");
printf("%d", res[j]);
count++;
}
}
printf("\n");
}
void solve()
{
int i, k, flag = 1, m = 0, n = 0, count = 0;
if (strchr(buf1, '.') != NULL)
{
k = strchr(buf1, '.') - buf1;
i = strlen(buf1) - 1;
while ('0' == buf1[i])
i--;
count = i - k;
}
else
flag = 0;
int c = strlen(buf1) - 1;
while (0!=flag && '0' == buf1[c])
c--;
for (i = 0; i <= c; i++)
{
if ('.' == buf1[i])
continue;
m = m * 10 + buf1[i] - '0';
}
n = atoi(buf2);
calc(n, m, count * n);
}
int main()
{
while (EOF != scanf("%s %s", buf1, buf2))
solve();
return 0;
}