POJ 2389 Bull Math(大数乘法,还是Java好)

本文介绍了一个名为BullMath的问题,该问题是关于大数乘法的编程挑战。任务要求参与者实现自己的大数乘法算法,而不使用任何内置的大数处理库。文章提供了两种解决方案:一种使用Java的BigInteger类,另一种则是纯手工实现的乘法过程。

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

Bull Math
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14252 Accepted: 7350

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

Source



大数乘法,数据只有一组!

AC代码:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			BigInteger a = sc.nextBigInteger();
			BigInteger b = sc.nextBigInteger();
			System.out.println(a.multiply(b));
		}
	}

}

AC代码2:

#include <stdio.h>
#include <string.h>
#define MAX 220
int main()
{
    char s1[MAX],s2[MAX];
    int a1[MAX],a2[MAX],p[2*MAX];;
    int i,j,len_1,len_2;
    memset(a1,0,sizeof(a1));
    memset(a2,0,sizeof(a2));
    memset(p,0,sizeof(p));
    gets(s1);
    gets(s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    for (j=0,i=len_1-1;i>=0;i--)
    {
        a1[j++]=s1[i]-'0';
    }

    for (j=0,i=len_2-1;i>=0;i--)
    {
        a2[j++]=s2[i]-'0';
    }

    for (i=0;i<len_1;i++)
    {
        for (j=0;j<len_2;j++)
        {
            p[i+j]+=a1[i]*a2[j];
        }
    }
    for (i=0;i<MAX*2;i++)
    {
        if(p[i]>9)
        {
            p[i+1]+=p[i]/10;
            p[i]%=10;
        }
    }

    int start=0;
    for (i=MAX*2-1;i>=0;i--)
    {
        if(start)
        {
            printf("%d",p[i]);
        }
        else if(p[i])
        {
            printf("%d",p[i]);
            start=1;
        }
    }
    if(!start)
        printf("0");

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值