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.
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;
}

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

被折叠的 条评论
为什么被折叠?



