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. spring mvc 异常处理和session添加

    在controller中进行设置 package com.stone.controller; import java.util.HashMap; import java.util.Map; impor ...

  2. C++中的RAII技法

    Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life ...

  3. Bootstrap入门(十六)组件10:well和具有响应式特性的嵌入内容

    Bootstrap入门(十六)组件10:well和具有响应式特性的嵌入内容 well组件可以为内容增添一种切入效果. 具有响应式特性的嵌入内容可以根据被嵌入内容的外部容器的宽度,自动创建一个固定的比例 ...

  4. 告别被拒,如何提升iOS审核通过率(下篇)——应用内容检查大法与提审资源检查大法

    WeTest 导读 之前的<告别被拒,如何提升iOS审核通过率(上篇)>分享了客户端检查的相关要点,本篇会给大家介绍有关应用内容的检查项和提审资源相关检查项要点. 应用内容检查大法 苹果对 ...

  5. 纯css实现轮播图

    轮播图的实现原理其实是比较简单的 举个例子 <div class="main"> <div class="div-main"></ ...

  6. 应用程序初次运行数据库配置小程序(Java版)

    应用程序初始化数据库配置小程序 之前写过一个Java版的信息管理系统,但部署系统的时候还需要手动的去配置数据库和导入一些初始化的数据才能让系统运行起来,所以我在想是不是可以写一个小程序在系统初次运行的 ...

  7. ubuntu服务器远程连接xshell,putty,xftp的简单使用教程

    当你自己千辛万苦终于搞到一个服务器(ubuntu(linux)系统的)之后,却不知道怎么进行时,xshell,putty,xftp是个很不错的选择 xshell和xftp是win下访问ubuntu(l ...

  8. .NET Core 最小化发布

    .NET Core 应用最小化独立部署发布,.NET Core 默认应用独立发布,大概占用50m左右的空间,不同的系统大小有所区别. .NET Core 的发布之前我也有所介绍,.NET Core 跨 ...

  9. Exiting the Matrix: Introducing Metasploit's Hardware Bridge

    Metasploit is an amazing tool. You can use it to maneuver through vast networks, pivoting through se ...

  10. C#npoi导出excel一些自己的看法

    之前转过一篇类似的文章,那个是用C#自带的excel类操作Excel的,还有一种在c#上操作excel的方法便是npoi了,npoi是poi的C#版本. npoi操作excel有两种形式,一种是hss ...