输入将包含一组双值R和n。R值将占用1至6列,和n值将在8和9列。
输出将包含一行每一行的输入给R ^ n的精确值。前导零应该抑制输出。无关紧要的尾随零不能打印出来。不打印小数点如果结果是一个整数。
样例输入
-
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
样例输出
-
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
#include <stdio.h>
#include <string.h>
int len; //total length of exponentiation result.
int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126
void multiply(int a[], int n)
{
int i;
int carry = 0; // a carry number in multiplying
for (i = 0; i < len; i++)
{
int temp = a[i]*n + carry;
a[i] = temp % 10;
carry = temp / 10;
}
while (carry)
{
a[i++] = carry % 10;
carry /= 10;
}
len = i;
}
int main(int argc, char* argv[])
{
int n; // power n
char s[6]; // real number R, at most the length is 6
while (scanf("%s %d", s, &n) != EOF)
{
int position=0, i=0, num=0, j=0;
for (i=0; i<strlen(s); i++)
{
if (s[i] == '.')
{
position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n
}
else
{
num = num*10 + s[i] - 48; // transfer float to integer
}
}
// product calculation
product[0]=1;
len = 1;
for (i = 0; i < n; i++)
{
multiply(product, num);
}
// format output
if (len <= position) // product is less than 1
{
printf("."); // print decimal point
for (i=0; i<position-len; i++)
{
printf("0"); // print zero between decimal point and decimal
}
j = 0;
//while (product[j] == 0) // trim trailing zeros
//{
// j++;
//}
for (i=len-1; i>=j; i--)
{
printf("%d", product[i]);
}
}
else
{
j=0;
while (product[j]==0 && j<position) // trim trailing zeros
{
j++;
}
for (i=len-1; i>=j; i--)
{
if (i+1 == position) // cause index in C language starts from 0
{
printf(".");
}
printf("%d", product[i]);
}
}
printf("\n");
}
}