poj1001 Exponentiation 解题报告

本文介绍了一个高精度小数乘法的实现方法,通过C语言编程详细展示了如何处理带有小数点的高精度数值乘法运算。该程序能够处理多位小数,并正确输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:带小数高精度乘法

好久没写过高精度了,还是挺练手的,注意输出的格式。之前WA了一次,看了下discuss里的数据才AC

#include<cstdio>
#include<cstring>
int c[200],a[200],b[200],x,n,len,lenx,xx;
char st[10];
void input(char *st)
{
    int i;
    n = 0;
    xx = x;
    lenx = 0;
    for (i = 5; i >= 0; i--)
        if (st[i] != '.')
        {
            ++n;
            b[n] = a[n] = st[i]-'0';
        }
        else lenx = 5-i;
}
void work()
{
    int i,j;
    memset(c,0,sizeof(c));
    len = n;
    x--;
    while (x--)
    {
        for (i = 1; i <= len; i++)
            for (j = 1; j <= 5; j++)
                c[i+j-1] += a[i]*b[j];
        len = len+5;
        for (i = 1; i <= len; i++)
        {
            c[i+1] = c[i+1] + c[i]/10;
            c[i] = c[i] % 10;
        }
        while (c[len] == 0) len--;
        for (i = 1; i <= len; i++)
        {
            a[i] = c[i];
            c[i] = 0;
        }
    }
    while (a[len] == 0) len--;
}
void output()
{
    int i,k;
    k = 1;
    while (a[k] == 0 && k <=lenx*xx) k++;
    for (i = lenx*xx; i > len; i--)
    {
        if (i == xx*lenx) printf(".");
        printf("0");
    }
    for (i = len; i >= k; i--)
    {
        if (i == xx*lenx) printf(".");
        printf("%d",a[i]);
    }
    printf("\n");
}
int main()
{
    while(scanf("%s%d",st,&x) == 2)
    {
        input(st);
        work();
        output();
    }
}
附上discuss里的BT调试数据:

/*

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
.00001  1
.12345  1
0001.1  1
1.1000  1
10.000  1
000.10  1
000000  1
000.00  1
.00000  0
000010  1
000.10  1
0000.1  1
00.111  1

0.0001  1
0.0001  3
0.0010  1
0.0010  3
0.0100  1
0.0100  3
0.1000  1
0.1000  3
1.0000  1
1.0000  3
1.0001  1
1.0001  3
1.0010  1
1.0010  3
1.0100  1
1.0100  3
1.1000  1
1.1000  3
10.000  1
10.000  3
10.001  1
10.001  3
10.010  1
10.010  3
10.100  1
10.100  3
99.000  1
99.000  3
99.001  1
99.001  3
99.010  1
99.010  3
99.100  1
99.100  3
99.998  1
99.998  3

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值