GVKun编程网logo

java Integer 大数据运算(java大数运算biginteger)

33

在这里,我们将给大家分享关于javaInteger大数据运算的知识,让您更了解java大数运算biginteger的本质,同时也会涉及到如何更有效地1005大数加法——51Nod(javaBigInt

在这里,我们将给大家分享关于java Integer 大数据运算的知识,让您更了解java大数运算biginteger的本质,同时也会涉及到如何更有效地1005 大数加法 ——51Nod(java BigInteger)、Integer Inquiry_hdu_1047(大数).java、Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理、Integer.valueOf、 Integer.parseInt 、 new Integer的内容。

本文目录一览:

java Integer 大数据运算(java大数运算biginteger)

java Integer 大数据运算(java大数运算biginteger)

 

 

package com.oracle.demo01;

import java.math.BigDecimal;
import java.math.BigInteger;

public class DemoInteger{
    public static void main(String[] args) {
        //四则运算:BigInteger 大的数据的运算
        BigInteger b1=new BigInteger("100000000000000000000000");
        BigInteger b2=new BigInteger("900000000000000000000000");
        //加法
        System.out.println(b1.add(b2));
        //减法
        System.out.println(b2.subtract(b1));
        //乘法
        System.out.println(b1.multiply(b2));
        //除法
        System.out.println(b2.divide(b1));
        
        
        
        //BigDecimal 高精度小数运算
        BigDecimal b3=new BigDecimal("0.09");
        BigDecimal b4=new BigDecimal("0.01");
        //加法
        System.out.println(b3.add(b4));
        //减法
        System.out.println(b3.subtract(b4));
        //乘法
        System.out.println(b3.multiply(b4));
        //除法
        System.out.println(b3.divide(b4));
                
        //减法
        BigDecimal a1=new BigDecimal("1.0");
        BigDecimal a2=new BigDecimal("0.32");
        System.out.println(1.0 - 0.32);
        System.out.println(a1.subtract(a2));
        //乘法
        BigDecimal a3=new BigDecimal("1.015");
        BigDecimal a4=new BigDecimal("100");
        System.out.println(1.015 * 100);
        System.out.println(a3.multiply(a4));
        //除法
        BigDecimal a5=new BigDecimal("1.301");
        BigDecimal a6=new BigDecimal("100");
        System.out.println(1.301 / 100);
        System.out.println(a5.divide(a6));
                
    }
}

 

.

1005 大数加法 ——51Nod(java BigInteger)

1005 大数加法 ——51Nod(java BigInteger)

基准时间限制:1 秒 空间限制:131072 KB

给出2个大整数A,B,计算A+B的结果。

Input
第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)

Output
输出A + B

Input示例
68932147586
468711654886

Output示例
537643802472

思路:
用java的BigInteger来写比较省事(其实就是高精度运算菜的扣脚~唉)

代码:

import java.util.*;
import java.math.*;

public class Main {

    /** * @param args */
    public static void main(String[] args) {
        // Todo Auto-generated method stub
        Scanner in = new Scanner(system.in);
        BigInteger big1 = in.nextBigInteger();
        BigInteger big2 = in.nextBigInteger();
        System.out.println(big1.add(big2));
    }

}

Integer Inquiry_hdu_1047(大数).java

Integer Inquiry_hdu_1047(大数).java

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9376    Accepted Submission(s): 2406


Problem Description
One of the first users of BIT''s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking varIoUs sums of those numbers. 
``This supercomputer is great,'''' remarked Chip. ``I only wish Timothy were here to see these results.'''' (Chip moved to a new apartment,once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
 

Input
The input will consist of at most 100 lines of text,each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length,and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself.
 

Output
Your program should output the sum of the VeryLongIntegers given in the input. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N,then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

Sample Input
  
  
1 123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
 

Sample Output
  
  
370370367037037036703703703670
 

Source
East Central North America 1996
 

import java.math.BigInteger;
import java.util.Scanner;

public class Main {//没咋看懂是怎么输入的
	public static void main(String[] args) {
		Scanner input=new Scanner(system.in);
		while(input.hasNext()){
			int n=input.nextInt();
			while(n-->0){
				BigInteger sum=BigInteger.valueOf(0);
				BigInteger a;
				while(!(a=input.nextBigInteger()).equals(BigInteger.valueOf(0))){
					sum=sum.add(a);
				}
				System.out.println(sum);
				if(n>0)
					System.out.println();
			}
		}
	}
}

Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理

Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理

先上代码:

int a = Integer.MAX_VALUE;
int b = Integer.MIN_VALUE;		
System.out.println(a + a);  // -2
System.out.println(b + b);  // 0
System.out.println(a - b);  // -1 

为什么是这样的结果?参考这篇博客《原码, 反码, 补码 详解》(http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html)

这样就好理解了,假设Max值为:01111111 假设Min值为:11111111,有以下结论:

max:		min:
0 1111111	1 1111111	| 原码
0 1111111	1 0000000	| 补码

min + min:
0 0000000	| 补码
0 0000000	| 原码 = 0

max + max:
1 1111110	| 补码
1 0000010	| 原码 = -2


max + min:
1 1111111	| 补码
1 0000001	| 原码 = -1

 

Integer.valueOf、 Integer.parseInt 、 new Integer

Integer.valueOf、 Integer.parseInt 、 new Integer

先看一下下面的结果

1.System.out.println(127==127); //true , int type compare2.System.out.println(128==128); //true , int type compare3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache7.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true , int type compare
登录后复制

 

解释

int整型常量比较时,== 是值比较,所以1,2返回true。1,2是值比较。

new Integer() 每次构造一个新的Integer对象,所以3返回false。3是对象比较。

Integer.parseInt每次构造一个int常量,所以4返回true。4是值比较。

Integer.valueOf返回一个Integer对象,默认在-128~127之间时返回缓存中的已有对象(如果存在的话),所以5返回true,6返回false。5,6是对象比较。

第7个比较特殊,是int 和 Integer之间的比较,结果是值比较,返回true。

 

总结

对于整型的比较,首先判断是值比较还是对象比较,值比较肯定返回true,有一个是值就是值比较。对象比较,则看对象是怎么构造出来的,如果是采用new Integer方式,则每次产生新对象,两个new出来的Integer比较肯定返回false,如果是Integer.valueOf方式的话,注意值的区间是否在-128~127之间,如果在,则构造的相同值的对象是同一个对象,==比较后返回true,否则返回false。

所以,对于值比较==放心没问题,对于Integer的比较最好用equals方法比较对象内容,当然注意先判断Integer是否不为null。

 

知识扩展

针对Integer.valueOf源码分析一下

1.我们调用的Integer.valueOf方法, 它先调用parseInt转成int型数值,再调它自己的重载方法

public static Integer valueOf(String s) throws NumberFormatException {return Integer.valueOf(parseInt(s, 10));
    }
登录后复制

2.Integer.valueOf重载方法,根据数值i的大小,决定是否从缓存中取一个Integer对象

 public static Integer valueOf(int i) {if (i &gt;= IntegerCache.low &amp;&amp; i 
登录后复制

3.Integer的构造函数,非常简单

  public Integer(int value) {this.value = value;
    }
登录后复制

4.IntegerCache静态类,是Integer的内部类,三个属性(一个缓存的Integer型数组+一组缓存范围)

private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by property(最大值可配置)int h = 127;
            String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); //读取VM参数if (integerCacheHighPropValue != null) {try {int i = parseInt(integerCacheHighPropValue); //配置值转换成int数值i = Math.max(i, 127);  //和127比较,取较大者// Maximum array size is Integer.MAX_VALUE(控制缓存数组的大小,最大为整型的最大值,这样一来,h值就必须小于整型最大值,因为要存 -128~0这129个数嘛)h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //实际就是 h=Math.min(i,Integer.MAX_VALUE-129),正整数能缓存的个数} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.                }
            }
            high = h;

            cache = new Integer[(high - low) + 1]; //构造缓存数组int j = low;for(int k = 0; k = 127; //如果小于127,抛异常}private IntegerCache() {}
    }
登录后复制

 看完上述Integer.valueOf源码后,你就会发现,默认的Integer缓存int常量池是可以配置的,配置方法是添加VM参数,加: -Djava.lang.Integer.IntegerCache.high=200

Intellij IDEA 运行配置的VM Options选项中添加参数即可。

 

以上就是Integer.valueOf、 Integer.parseInt 、 new Integer的详细内容,更多请关注php中文网其它相关文章!

关于java Integer 大数据运算java大数运算biginteger的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于1005 大数加法 ——51Nod(java BigInteger)、Integer Inquiry_hdu_1047(大数).java、Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理、Integer.valueOf、 Integer.parseInt 、 new Integer等相关内容,可以在本站寻找。

本文标签:

上一篇4、Java的运算符(java中运算符)

下一篇Java IO详解(七)------随机访问文件流(java随机访问文件功能)