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

 
 
已经最近一直在工作了。很少机会刷题了,所以趁今天有水了一道题。
就是最简单的大数乘法(此题数据不强,没有多余0开头的数字和没有negative integer~~).
看到大数运算,第一想法是果断用Java神功护体,BigDecimal!!!
但是这样没什么意思呀。。所以想了想就自己写了,水题嘛也不要太水过了~
 
其实大数运算的核心思想都是用数组来储存每一个位置上的数,这样只要数组足够大,理论上可以计算无穷大的运算。因为每个数字上(譬如int)都不超过10,所以储存下来卓卓有余。
然后就模拟我们小学的加减乘除运算。就得出答案了~
 
Java Code Here:
import java.util.Scanner;

public class Main {

    public static void main(String[] args){
Scanner sc = new Scanner( System.in );
BigMultiplicative bm = new BigMultiplicative( 500 );
while(sc.hasNext()){
String a = sc.next();
String b = sc.next();
System.out.println( bm.doMultiplicative( a.toCharArray(), b.toCharArray() ) ); }
}
} class BigMultiplicative { private int[] answer; private int capacity; private int length = 0; public int getCapacity() {
return capacity;
} public void setCapacity( int capacity ) {
this.capacity = capacity;
} public int getLength() {
return length;
} public void setLength( int length ) {
this.length = length;
} public BigMultiplicative( int capacity ) {
this.capacity = capacity;
} public String doMultiplicative(char[] a,char[] b){
answer = new int[capacity];
String as = String.valueOf( a );
String bs = String.valueOf( b );
for(int i=as.length()-1;i>=0;i--){
for(int j=bs.length()-1;j>=0;j--){
int index = bs.length() - j -1 + (as.length()-1-i);
int temp = Integer.parseInt(String.valueOf(as.charAt( i ))) * Integer.parseInt(String.valueOf(bs.charAt( j )));
int over = temp / 10;
answer[index] += temp%10;
if(answer[index] >= 10){
int carry = answer[index];
answer[index] = ( char )( answer[index] % 10 );
answer[index+1]+=carry/ 10;
}
if(over!=0){
answer[index +1 ] += over;
if(index +1 > length){
length = index+1;
}
}
if(index > length){
length = index;
}
}
}
if(answer[length+1] != 0){
length++;
}
StringBuilder sb = new StringBuilder();
for(int i=length;i>=0;i--){
sb.append( (int)answer[i] );
}
for(int i=0;i<sb.length();i++){
if(sb.charAt( i) == '0'){
sb.deleteCharAt( 0 );
i--;
}else{
break;
}
}
return sb.toString();
} }
 

[PKU2389]Bull Math (大数运算)的更多相关文章

  1. POJ2389 Bull Math【大数】

    Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15040   Accepted: 7737 Descri ...

  2. Poj OpenJudge 百练 2389 Bull Math

    1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...

  3. 收藏的一段关于java大数运算的代码

    收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...

  4. java 大数运算[转]

    用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...

  5. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  6. lua实现大数运算

    lua实现的大数运算,代码超短,眼下仅仅实现的加减乘运算 ------------------------------------------------ --name: bigInt --creat ...

  7. 大数运算之 Java BigInteger 的基本用法

    大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...

  8. 大数运算(python2)

    偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...

  9. BZOJ1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 374  Solved: 227[Submit ...

随机推荐

  1. C#中如何使用IComparable<T>与IComparer<T>接口(转载)

    本分步指南描述如何使用两个接口: IComparer和IComparable.在同一篇文章中讨论这些接口有两个原因.经常在一起,使用这些接口和接口类似 (并且有相似的名称),尽管它们用于不同用途. 如 ...

  2. HDU-1232-畅通工程(并查集)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1232考察并查集,(最小生成树)题目很简单用k记录树根的个数,k-1就是还需要建设的路 #include& ...

  3. 上传预览 easyui部分控件获取focuse 表单验证

    js: $(document).ready(function () { //$('#creater').combobox({ // url: '/VMS.UI/BindData/ScheamData? ...

  4. JSON数据格式中的引号

    JSON数据中必须使用双引号: $.getJSON,的输入必须是正确的JSON数据,否则不会执行回调函数: $.parseJSON的输入必须是正确的JSON数据,否则会有异常:

  5. 算法一之N皇后问题

    (写这篇文章主要是明天就要考试了,算法考试,今天不想再复习了,xiang着今天也开通了博客,于是在这个平台上进行复习,应该会更高效.最后祝愿我明天考个好成绩.嘻嘻...) n皇后问题,主要是应用到回溯 ...

  6. 在ubuntu下编写python(python入门)

    在ubuntu下编写python 一般情况下,ubuntu已经安装了python,打开终端,直接输入python,即可进行python编写. 默认为python2 如果想写python3,在终端输入p ...

  7. ArcGIS API for JavaScript 4.2学习笔记[2] 显示3D地图

    3D地图又叫场景. 由上一篇可知, require入口函数的第一个参数是字符串数组 ["esri/Map", "esri/views/MapView", &qu ...

  8. [2017.02.04] C++学习记录(1)

    编编程语言的目的是帮助程序员以代码的形式表述ideas.编程语言一方面为程序员提供一组关于可以做什么的抽象,另一方面为程序员提供可以被机器执行的轮子.C++编程语言,支持4种编程范式:过程式(Proc ...

  9. 源码(09) -- java.util.Arrays

    java.util.Arrays 源码分析 ------------------------------------------------------------------------------ ...

  10. asp.net权限认证篇外:集成域账号登录

    在之前的我们已经讲过asp.net权限认证:Windows认证,现在我们来讲讲域账号登录, 这不是同一件事哦,windows认证更多的是对资源访问的一种权限管控,而域账号登录更多的是针对用户登录的认证 ...