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. 滚轮事件的防冒泡、阻止默认行为的代码(效果是:只让当前div滚动,连当前文档都不滚动的效果)

    //用firefox变量表示火狐代理var firefox = navigator.userAgent.indexOf('Firefox') != -1;function MouseWheel(e){ ...

  2. log4net的分类型输出文件的配置

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  3. LINQ 的查询_联表、分组、排序

    1.查询 var v = from s in db.Set<ScoreInfo>().ToList()group s by s.subject into scoreselect new{  ...

  4. 关于Vue.js 使用v-cloak后仍显示变量的解决方法

    v-cloak   这个指令是防止页面加载时出现 vuejs 的变量名而设计的,但有时候添加了这个指令仍会显示变量,这是怎么回事呢?. v-cloak 用法: HTML代码: <div v-cl ...

  5. 在windows下运行spark

    1.下载spark:spark-2.0.0-bin-hadoop2.7.tgz 2.解压至D:\bigdata\spark-2.0.0-bin-hadoop2.7 3.配置环境变量 HADOOP_HO ...

  6. MAC的VIMRC

    set nocompatible            " 关闭 vi 兼容模式 syntax on                   " 自动语法高亮 " color ...

  7. 【开源.NET】 分享一个前后端分离的轻量级内容管理框架

    开发框架要考虑的面太多了:安全.稳定.性能.效率.扩展.整洁,还要经得起实践的考验,从零开发一个可用的框架,是很耗时费神的工作.网上很多开源的框架,为何还要自己开发?我是基于以下两点: 没找到合适的: ...

  8. DTLS 技术要点解析

    一.DTLS DTLS 是指 Datagram Transport Level Security,即数据报安全传输协议: 其提供了UDP 传输场景下的安全解决方案,能防止消息被窃听.篡改.身份冒充等问 ...

  9. Linux Platform驱动模型(二) _驱动方法

    在Linux设备树语法详解和Linux Platform驱动模型(一) _设备信息中我们讨论了设备信息的写法,本文主要讨论平台总线中另外一部分-驱动方法,将试图回答下面几个问题: 如何填充platfo ...

  10. RAS 加密 解密

    蚂蚁金服电话面试时,问到了RAS加密解密,感觉回答的有点模糊,遂写个例子加深一下印象 package cheng.test.cipher;import java.io.FileInputStream; ...