poj1001 Exponentiation 解题报告

来源:互联网 发布:mac os x系统 编辑:程序博客网 时间:2024/05/16 14:05

题意:带小数高精度乘法

好久没写过高精度了,还是挺练手的,注意输出的格式。之前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 120.4321 205.1234 156.7592  998.999 101.0100 12.00001  1.12345  10001.1  11.1000  110.000  1000.10  1000000  1000.00  1.00000  0000010  1000.10  10000.1  100.111  10.0001  10.0001  30.0010  10.0010  30.0100  10.0100  30.1000  10.1000  31.0000  11.0000  31.0001  11.0001  31.0010  11.0010  31.0100  11.0100  31.1000  11.1000  310.000  110.000  310.001  110.001  310.010  110.010  310.100  110.100  399.000  199.000  399.001  199.001  399.010  199.010  399.100  199.100  399.998  199.998  3

*/

原创粉丝点击