[高精度] Poj2389 Bull Math

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值