POJ 2389 Bull Math(大数乘法,还是Java好)
2016-05-14 00:44
531 查看
Bull Math
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
Sample Output
Source
USACO 2004 November
原题链接:http://poj.org/problem?id=2389
大数乘法,数据只有一组!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14252 | Accepted: 7350 |
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; }
相关文章推荐
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
- Java学习之static关键字
- java多线程系列1--线程实现与调度
- JAVA文件中获取路径及WEB应用程序获取路径方法
- Java获取WEB目录路径
- java-web易错汇总
- Java设计模式之策略设计模式
- java jdbc编程,运行总显示不允许链接
- Java泛型 泛型的上下限
- Spring Securty 4.1.0 基本概念说明
- SSM框架 SSM项目源码 SSM源码 下载 java框架整合Springmvc+mybatis+s
- myeclipse里的Hibernate关系映射
- Java之properties文件读取
- Java获得ip地址
- Java判断是不是汉字、中文、英文字母和数字
- Java获取格式化时间
- 简单倒排索引Java实现
- java环境搭建
- 学习java编程应该会的第一个Demo
- java学习计划