Problem 1453 Bignum Arithmetic from http://acm.fzu.edu.cn/problem.php?pid=1453

本文介绍了一种处理大数乘法的方法,通过自定义数组来存储超过常规整型变量所能表示的大数,并实现了两个大数之间的乘法运算。文章提供了一个具体的C++代码示例,展示了如何读取两个大数、执行乘法操作并打印结果。

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

 Problem 1453 Bignum Arithmetic

Accept: 399    Submit: 1028
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

In this problem, you will be concerned with integers with very large numbers of digits. You must write code which will repeatedly accept (until end of file) two lines each containing an unsigned integer, and output the product of the two input unsigned integers. The output must not contain any leading zeros.

You can assume that each integer will contain at most 80 digits. The input ends with an end of file.

 Sample Input

03421298123

 Sample Output

44391636


#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int a[100];
int b[100];
char s1[100];
char s2[100];
int c[200];
int lena, lenb, i, j, len;
void multi(){
    memset(c, 0, sizeof(c));
    len = lena+lenb;

    for(i=0;i<lena;++i)
        for(j=0;j<lenb;++j){
            c[i+j] += a[i]*b[j];
        }
    for(i=0;i<len;++i){
        c[i+1] += c[i]/10;
        c[i] %= 10;
    }

    len++;
    while(len>0 && !c[len-1])
        len--;

}
int main(){


	//freopen("in.txt", "r", stdin);
    while(cin>>s1>>s2){
        lena = strlen(s1);
        lenb = strlen(s2);
        for(i=lena-1;i>=0;--i)
            a[lena-1-i] = s1[i] - '0';
        for(i=lenb-1;i>=0;--i)
            b[lenb-1-i] = s2[i] - '0';
        multi();
        if(len==0)
            printf("0");
        else
            for(i=len-1;i>=0;i--)
                cout<<c[i];
        printf("\n");
    }

	//fclose(stdin);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值