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

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

USACO 2004 November

原题链接:http://poj.org/problem?id=2389

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

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;
}
时间: 2024-10-06 00:23:32

POJ 2389 Bull Math(大数乘法,还是Java好)的相关文章

POJ 2389 Bull Math(大数相乘)

Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13920   Accepted: 7192 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. F

poj 2389.Bull Math 解题报告

题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次写 = =.......大整数加法写得比较多.百练也有一条是大整数乘法,链接如下:http://bailian.openjudge.cn/practice/2980/ 一步一步模拟即可,代码就是按这个来写的. 以 835 * 49 为例(亲爱的读者,允许我截图吧) 简直就是神奇呀----- 1 #i

POJ 2389 Bull Math

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13500   Accepted: 6968 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

POJ 1503 Integer Inquiry(大数相加,java)

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

51 Nod 1027 大数乘法【Java大数乱搞】

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1027 分

51 Nod 1028 大数乘法 V2【Java大数乱搞】

1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 100000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemI

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 某人贴出的測试用例数据地址: http://poj.org/showmessage?message_id=76017 有了这些用例,几下调试就过了. 我关键漏了的用例: 000.10  1 000000  1 000.00  1 .00000  0 0000