原文链接

Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类。

这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

Ⅰ基本函数:

1.valueOf(parament);//将参数转换为制定的类型

2.add(); //大整数相加

3.subtract(); //相减

4.multiply(); //相乘

5.divide(); //相除取整

6.remainder(); //取余

7.pow(); //a.pow(b)=a^b

8.gcd(); //最大公约数

9.abs(); //绝对值

10.negate(); //取反数

11.mod(); //a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.public int comareTo();

14.boolean equals();//是否相等

15.//BigInteger构造函数:

BigInteger(String val);

//将指定字符串转换为十进制表示形式;

BigInteger(String val,int radix);

//将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

Ⅱ.基本常量:

A=BigInteger.ONE 1

B=BigInteger.TEN 10

C=BigInteger.ZERO 0

Ⅲ.基本操作——读入与输出

用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNext()) //等同于!=EOF
{
int n;
BigInteger m;
n=cin.nextInt(); //读入一个int;
m=cin.BigInteger();//读入一个BigInteger;
System.out.print(m.toString());
}

Ⅳ.四则运算:

import java.util.Scanner;
import java.math.*;
import java.text.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner ( System.in );
BigInteger a,b;
int c;
char op;
String s; while( cin.hasNext() ) {
a = cin.nextBigInteger();
s = cin.next();
op = s.charAt(0);
if( op == '+') {
b = cin.nextBigInteger();
System.out.println(a.add(b));
}
else if( op == '-') {
b = cin.nextBigInteger();
System.out.println(a.subtract(b));
}
else if( op == '*') {
b = cin.nextBigInteger();
System.out.println(a.multiply(b));
}
else {
BigDecimal a1,b1,eps;
String s1,s2,temp;
s1 = a.toString();
a1 = new BigDecimal(s1);
b = cin.nextBigInteger();
s2 = b.toString();
b1 = new BigDecimal(s2);
c = cin.nextInt();
eps = a1.divide(b1,c,4);
//System.out.println(a + " " + b + " " + c);
//System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);
System.out.print( a.divide(b) + " " + a.mod(b) + " ");
if( c != 0) {
temp = "0.";
for(int i = 0; i < c; i ++) temp += "0";
ecimalFormat gd = new DecimalFormat(temp);
System.out.println(gd.format(eps));
}
else System.out.println(eps);
}
}
}
}

具体解释:

1.valueOf(parament); 将参数转换为制定的类型

比如 int a=3;

BigInteger b=BigInteger.valueOf(a);

则b=3;

String s=”12345”;

BigInteger c=BigInteger.valueOf(s);

则c=12345;

2.add(); 大整数相加

BigInteger a=new BigInteger(“23”);

BigInteger b=new BigInteger(“34”);

a.add(b);

3.subtract(); 相减

4.multiply(); 相乘

5.divide(); 相除取整

6.remainder(); 取余

7.pow(); a.pow(b)=a^b

8.gcd(); 最大公约数

9.abs(); 绝对值

10.negate(); 取反数

11.mod(); a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.public int comareTo();

返回-1,0或1,分别为BigInteger在数字上小于,等于,或大于值val。

14.boolean equals(); 是否相等

15.BigInteger构造函数:

一般用到以下两种:

BigInteger(String val);

将指定字符串转换为十进制表示形式;

BigInteger(String val,int radix);

将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

补充:

a=a.pow(b); //a = a的b次方

a=a.stripTrailingZeros(); //去掉后导0

d=a.toPlainString(); //不让其变成科学计数法的表示法,变成一般的小数表示

if(d.charAt(0)==’0’) d=d.substring(1); //与前导的字符串比较 ??

if(s.startsWith(“0.”)) s=s.substring(1);//与前导的字符串比较 ??

and(); // 例如a.and(b),计算a&b

doubleValue(); //转化为double类型

longValue(); //转化为long类型

floatValue(); // 转化为float类型

intValue(); //转化为int类型

divideAndRemainder(BigInteger val)

//返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。

boolean isProbablePrime(BigInteger n) //判断大数是否为素数

modPow(BigInteger n, BigInteger mod) //计算this^n % mod

nextProbablePrime(BigInteger n) //返回比大数n大的为素数的数

not() //计算 ~this

or(BigInteger a) //计算this|a

xor(BigInteger val) //返回其值为 (this ^ val) 的 BigInteger。

probablePrime(int bitLength, Random rnd)

//返回有可能是素数的、具有指定长度的正 BigInteger。

shiftLeft(int n) //返回其值为 (this << n) 的 BigInteger。

shiftRight(int n) //返回其值为 (this >> n) 的 BigInteger。

JAVA大数处理(BigInteger,BigDecimal)的更多相关文章

  1. Java 大数类BigInteger和BigDecimal的基本函数

    在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运 ...

  2. java大数(BigInteger)

    JAVA之BigInteger 用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的 ...

  3. Java大数类 BigInteger

    package bigint; /** * This class encapsulates a BigInteger, i.e. a positive or negative integer * wi ...

  4. 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】

    Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...

  5. JAVA - 大数类详解

    写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...

  6. java大数

    java大数还是很好用的! 基本加入: import java.math.BigInteger; import jave.math.BigDecimal; 分别是大数和大浮点数. 首先读入可以用: S ...

  7. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

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

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

  9. Java 大数、高精度模板

    介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...

随机推荐

  1. libevent编程疑难解答

    http://blog.csdn.net/luotuo44/article/details/39547391 转载请注明出处:http://blog.csdn.net/luotuo44/article ...

  2. Android反复闹钟(每天)的实现

    MainActivity例如以下: package cc.cc; import java.util.Calendar; import java.util.Locale; import android. ...

  3. 浅谈 ZipArchive 类

    Microsoft .NET Framework 4.5 新增了 ZipArchive 类 Microsoft Windows 8 Consumer Preview 操作系统已经内置了 Microso ...

  4. Android 使用图片异步载入框架Universal Image Loader的问题

    使用的Jar包 问题:        optionsm = new DisplayImageOptions.Builder()         .displayer(new RoundedBitmap ...

  5. 初探swift语言的学习笔记十一(performSelector)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/35842441 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...

  6. 一个JS引发的跨域问题

    忽然遇上跨域错误. 我们有张页面,使用了EXT.js,在本地运行正常,部署到服务器上,出不来数据.F12调试,提示有跨域错误? XMLHttpRequest cannot load http://19 ...

  7. MySQL迁移到SQLServer

    手头有个Java老项目,数据库是基于MySQL的,我们要把它迁移到SQLServer2008. 采用微软的SSMA For MySQL:迁移助手Microsoft SQL Server Migrati ...

  8. mongodb02

    memcached redis : kv数据库(key/value) mongodb 文档数据库,存储的是文档(Bson->json对象二进制化后叫bson,js的二进制对象,引擎是用js实现的 ...

  9. bzoj2823: [AHOI2012]信号塔&&1336: [Balkan2002]Alien最小圆覆盖&&1337: 最小圆覆盖

    首先我写了个凸包就溜了 这是最小圆覆盖问题,今晚学了一下 先随机化点,一个个加入 假设当前圆心为o,半径为r,加入的点为i 若i不在圆里面,令圆心为i,半径为0 再重新从1~i-1不停找j不在圆里面, ...

  10. 【转】Java 并发编程:核心理论

    并发编程是Java程序员最重要的技能之一,也是最难掌握的一种技能.它要求编程者对计算机最底层的运作原理有深刻的理解,同时要求编程者逻辑清晰.思维缜密,这样才能写出高效.安全.可靠的多线程并发程序.本系 ...