Exponentiation
Time Limit: 2000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9385 Accepted Submission(s): 2775
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 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
Source
Recommend
PrincetonBoy
讲真,这个题目,写的挺乱的,但是注释之后,应该能看懂,我是把大浮点数运算先转化为大整数运算,然后找出小数点的位置,然后逐位输出即可。
#include <stdio.h>
#include <string.h>long long f[30][200];
int lenth[30];
/*大数运算*/
void caculate(int n,long long key)
{
if (key >= 10000)
{
f[0][2] = key / 10000;
f[0][1] = key % 10000;
lenth[0] = 2;
}
else
{
f[0][1] = key;
lenth[0] = 1;
}
for (int i = 1; i < n; i++)
{
long long p = 0;
for (int j = 1; j <= lenth[i - 1]; j++)
{
f[i][j] = (f[i - 1][j] * key + p) % 10000;
p = (f[i - 1][j] * key + p) / 10000;
}
lenth[i] = lenth[i - 1];
if (p > 0)
{
while (p > 0)
{
lenth[i]++;
f[i][lenth[i]] = p % 10000;
p = p / 10000;
}
}
}
}
int main()
{
char a[10];
long long b[200];
int n;
while (scanf("%s%d", a, &n) != EOF)
{
long long key = 0;
int flag = 0, flag0 = 0;
int t = -1, i = strlen(a) - 1;
while (i >= 0)//判断输入是否有小数点
{
if (a[i] == '.')
{
flag0 = 1;
break;
}
--i;
}
i = strlen(a) - 1;
while (flag0 && a[i] == '0')//去除小数位无用的0,若无小数点则不需要去除
i--;
int p = 1, len = i;
while (i >= 0)
{
if (a[i] != '.')//转化为整数作计算
{
key = key + (a[i] - '0') * p;
p = p * 10;
}
else
t = i, flag = 1;
--i;
}
i = len;
while (i >= 0 && a[i] != '.')//判断输入是否为整数
{
if (a[i] != '0')//有小数点且小数点后全为0,则为整数
{
break;
}
i--;
}
if (i >= 0)
{
if (a[i] == '.')
flag = 0;
}
if (t != -1)
t = (len - t) * n;//求出小数的位数
caculate(n, key);
int k = 0;
for(int i = 1;i <=lenth[n - 1];i++)//求出各位的数字
{
int count = 4;
while (count--)
{
b[k] = f[n - 1][i] % 10;
f[n - 1][i] = f[n - 1][i] / 10;
++k;
}
}
k = k - 1;
while (b[k] == 0)//去除前导0
k--;
if (!flag)//为整数,则不带小数点输出
{
while (k >= 0)
{
printf("%lld", b[k]);
k--;
}
putchar('\n');
continue;
}
else
{
int mid = k - t + 1;//求出小数点应该输出的位置
if (mid <= 0)//整数位为0
{
printf(".");
while (mid != 0)
{
printf("0");
mid++;
}
while (k >= 0)
{
printf("%lld", b[k]);
k--;
}
putchar('\n');
}
else
{
for (int i = 1; i <= mid; i++)
{
printf("%lld", b[k]);
k--;
}
printf(".");
while (k >= 0)
{
printf("%lld", b[k]);
--k;
}
putchar('\n');
}
}
}
return 0;
}