POJ1001测试值位数相同,数值有偏差
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.
可以直接运行
下面是输入和输出
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
Hint
#include<stdio.h>
#include<string.h>
char *multiply1(char *numberN,char *numberM);
void mi();
int main()
{
/* char numberN[1500], numberM[1500];//为什么这里不能定义为char *num呢?
scanf("%s%s", numberN, numberM);
char *j;
j=multiply1(numberN,numberM);
puts(j);
*/
while(1)
mi();
}
void mi()
{
char di[1500];
int mici;
printf("输入底数,0.0-99.999;输入幂,1-25\n ");
scanf("%s",di);
scanf("%d",&mici);
int i;
char *qu[mici];//定义一个char 数组,用于存储不同幂次下的值
qu[0]=di;
for(i=0;i<mici-1;i++)
{
qu[i+1]=multiply1(qu[i],di);
}
puts(qu[mici-1]);
}
char *multiply1(char *numberN,char *numberM)//高精度乘法
{
int n1,n2;//n1,n2是小数点位置。
int i, j;
char w[1500];
int k=0;
int n = strlen(numberN), m = strlen(numberM);//求字符串长度
int a[n]={0}, b[m]={0};//这样就先把数组置为全0
for (i = 0, j = n - 1; j>=0; j--) {
if(numberN[j]!='.')//要找出小数点的位置
{
a[i] = numberN[j] - '0';
i++;
}
else
n1=i;
}
for (i = 0, j = m - 1;j>=0;j--) {
if(numberM[j]!='.')
{
b[i] = numberM[j] - '0';
i++;
}
else
n2=i;
}
int c[3000];
for (i = 0; i < 3000; i++) {
c[i] = 0;
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
c[i + j] += a[i] * b[j];
}
}
for (i = 0; i < n + m-1; i++) {//这里的总位数是否是n+m-1
if (c[i] >= 10) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
for (j = 2999; j > 0; j--) {
if (c[j] != 0)
break;
}
if(n1+n2>j)
{
w[0]='0';
w[1]='.';
k=2;
for(i=1;i<=n1+n2-j-1;i++)
{
w[k]='0';
k++;
}
for(i=j;i>=0;i--)
{
w[k]=48+c[i];
k++;
}
}
else
{
i=j;
while(i>=0)
{
if(i!=n1+n2-1)
{
w[k]=48+c[i];
i--;
k++;
}
else
{
w[k]='.';
k++;
w[k]=c[i]+48;
i--;
}
}
}
return w;
}