[高精度] Poj2389 Bull Math

来源:互联网 发布:java list clear 编辑:程序博客网 时间:2024/05/17 22:42

D - Bull Math
Time Limit:1000MS Memory Limit:65536KB
64bit IO Format:%I64d & %I64u

Status
Description
Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers … or so they say. Farmer John wonders if their answers are correct. Help him check the bulls’ answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).

FJ asks that you do this yourself; don’t use a special library function for the multiplication.

Input
* Lines 1..2: Each line contains a single decimal number.

Output
* Line 1: The exact product of the two input lines

Sample Input
11111111111111
1111111111

Sample Output
12345679011110987654321

题意很好理解,也可以看样例输入输出就知道了,大数乘法模板题。
具体重点看大数模板的输入,输出以及使用方法。

#include <iostream>#include<cstdio>#include<cstring>using namespace std;struct BigInt{    const static int mod = 10000;    const static int DLEN =4;    int a[600],len;    BigInt(){    memset(a,0,sizeof(a));    len = 1;    }    BigInt(const char s[])    {        memset(a,0,sizeof(a));        int L = strlen(s);        len = L/DLEN;        if(L%DLEN)len++;        int index = 0;        for(int i = L-1;i>=0;i-=DLEN)        {            int t = 0;            int k = i-DLEN +1;            if(k<0) k=0;            for(int j = k;j<=i;j++)                t = t*10 + s[j] - '0';            a[index++] = t;        }    }    BigInt operator *(const BigInt &b)const    {        BigInt res;        for(int i = 0;i<len; i++)        {            int up = 0;            for(int j = 0 ; j < b.len;j++)            {                int temp = a[i] * b.a[j] + res.a[i+j] +up;                res.a[i+j] = temp % mod;                up = temp / mod;            }            if (up != 0)                res.a[i+b.len] = up;        }        res.len = len+b.len;        while(res.a[res.len - 1] == 0 && res.len >1) res.len--;        return res;    }    void output()    {        printf("%d",a[len-1]);        for(int i = len-2; i>=0;i--)            printf("%04d",a[i]);        printf("\n");    }};int main(){    char  ch1[600];    char ch2[600];    scanf("%s",ch1);    scanf("%s",ch2);    BigInt A(ch1);    BigInt B(ch2);    BigInt C = A * B;    C.output();    return 0;}
0 0