#2
rjsp2023-09-20 08:44
|
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 <stdio.h>
#include <string.h>
#define length 200
char* mult(char *p, long m);
int main()
{
char str[length + 2];
char in[10];
int point;
long num;
int n;
while (scanf("%s%d", in, &n) != EOF)
{
memset(str, 0, length + 2);
int len = strlen(in);
point = 0;
for (int i = 0; i < 8; i++)
{
if (in[i] == '.')
{
point = i;
break;
}
}
if (point)
{
for (int i = point; i < len; i++)//暂去小数点,化成整数运算
{
in[i] = in[i + 1];
}
point = len - point - 1;//计算小说位数
}
sscanf(in, "%ld", &num);
if (num == 0)
{
printf("0\n");
continue;
}
char *p = str + length - 1;
*(p + 0) = '1';
for (int i = 0; i < n; i++)
{
p = mult(p, num);
}
point *= n;
if (point)
{
for (int i = length; i > length - point; i--)//添加小数点
str[i] = str[i - 1];
str[length - point] = '.';
for (int i = length; i > length - point; i--)//消除后位0
{
if (str[i] == '0')
str[i] = 0;
else break;
}
for (int i = length - point; i < length; i++)//避免小数点后无效
{
if (str[i + 1] == 0)
str[i + 1] = '0';
else break;
}
}
p = str;//重置p
while (*(p + 0) == '0' || *(p + 0) == '\0')//消除前导0
p++;
printf("%s\n", p);
}
}
char* mult(char *p, long m) //乘法函数
{
int len = strlen(p);
int i = len - 1;
long int n, n2 = 0/*进位*/;
while (i >= 0)
{
n = *(p + i) - '0';
n = n * m + n2;
n2 = n / 10;
n %= 10;
*(p + i) = n + '0';
i--;
}
while (n2 != 0)
{
*(p - 1) = n2 % 10 + '0';
n2 /= 10;
p--;
}
return p;
}
#include <string.h>
#define length 200
char* mult(char *p, long m);
int main()
{
char str[length + 2];
char in[10];
int point;
long num;
int n;
while (scanf("%s%d", in, &n) != EOF)
{
memset(str, 0, length + 2);
int len = strlen(in);
point = 0;
for (int i = 0; i < 8; i++)
{
if (in[i] == '.')
{
point = i;
break;
}
}
if (point)
{
for (int i = point; i < len; i++)//暂去小数点,化成整数运算
{
in[i] = in[i + 1];
}
point = len - point - 1;//计算小说位数
}
sscanf(in, "%ld", &num);
if (num == 0)
{
printf("0\n");
continue;
}
char *p = str + length - 1;
*(p + 0) = '1';
for (int i = 0; i < n; i++)
{
p = mult(p, num);
}
point *= n;
if (point)
{
for (int i = length; i > length - point; i--)//添加小数点
str[i] = str[i - 1];
str[length - point] = '.';
for (int i = length; i > length - point; i--)//消除后位0
{
if (str[i] == '0')
str[i] = 0;
else break;
}
for (int i = length - point; i < length; i++)//避免小数点后无效
{
if (str[i + 1] == 0)
str[i + 1] = '0';
else break;
}
}
p = str;//重置p
while (*(p + 0) == '0' || *(p + 0) == '\0')//消除前导0
p++;
printf("%s\n", p);
}
}
char* mult(char *p, long m) //乘法函数
{
int len = strlen(p);
int i = len - 1;
long int n, n2 = 0/*进位*/;
while (i >= 0)
{
n = *(p + i) - '0';
n = n * m + n2;
n2 = n / 10;
n %= 10;
*(p + i) = n + '0';
i--;
}
while (n2 != 0)
{
*(p - 1) = n2 % 10 + '0';
n2 /= 10;
p--;
}
return p;
}