[PKU2389]Bull Math (大数运算)
Description
FJ asks that you do this yourself; don't use a special library function for the multiplication.
Input
Output
Sample Input
11111111111111
1111111111
Sample Output
12345679011110987654321
Source
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 (大数运算)的更多相关文章
- POJ2389 Bull Math【大数】
Bull Math Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15040 Accepted: 7737 Descri ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- java 大数运算[转]
用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
- lua实现大数运算
lua实现的大数运算,代码超短,眼下仅仅实现的加减乘运算 ------------------------------------------------ --name: bigInt --creat ...
- 大数运算之 Java BigInteger 的基本用法
大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...
- 大数运算(python2)
偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...
- BZOJ1754: [Usaco2005 qua]Bull Math
1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 374 Solved: 227[Submit ...
随机推荐
- 滚轮事件的防冒泡、阻止默认行为的代码(效果是:只让当前div滚动,连当前文档都不滚动的效果)
//用firefox变量表示火狐代理var firefox = navigator.userAgent.indexOf('Firefox') != -1;function MouseWheel(e){ ...
- log4net的分类型输出文件的配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- LINQ 的查询_联表、分组、排序
1.查询 var v = from s in db.Set<ScoreInfo>().ToList()group s by s.subject into scoreselect new{ ...
- 关于Vue.js 使用v-cloak后仍显示变量的解决方法
v-cloak 这个指令是防止页面加载时出现 vuejs 的变量名而设计的,但有时候添加了这个指令仍会显示变量,这是怎么回事呢?. v-cloak 用法: HTML代码: <div v-cl ...
- 在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 ...
- MAC的VIMRC
set nocompatible " 关闭 vi 兼容模式 syntax on " 自动语法高亮 " color ...
- 【开源.NET】 分享一个前后端分离的轻量级内容管理框架
开发框架要考虑的面太多了:安全.稳定.性能.效率.扩展.整洁,还要经得起实践的考验,从零开发一个可用的框架,是很耗时费神的工作.网上很多开源的框架,为何还要自己开发?我是基于以下两点: 没找到合适的: ...
- DTLS 技术要点解析
一.DTLS DTLS 是指 Datagram Transport Level Security,即数据报安全传输协议: 其提供了UDP 传输场景下的安全解决方案,能防止消息被窃听.篡改.身份冒充等问 ...
- Linux Platform驱动模型(二) _驱动方法
在Linux设备树语法详解和Linux Platform驱动模型(一) _设备信息中我们讨论了设备信息的写法,本文主要讨论平台总线中另外一部分-驱动方法,将试图回答下面几个问题: 如何填充platfo ...
- RAS 加密 解密
蚂蚁金服电话面试时,问到了RAS加密解密,感觉回答的有点模糊,遂写个例子加深一下印象 package cheng.test.cipher;import java.io.FileInputStream; ...