ACM常用算法-大数四则,高精度(Java)

来源:互联网 发布:海森矩阵维基百科 编辑:程序博客网 时间:2024/06/16 10:55

这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math.BigInteger;和import java.math.BigDecimal;

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

下面从几个方面对BigInteger和BigDecima做一个简单的比较:

 

一.常量

BigInteger:ONE,ZERO,TEN分别代表1,0,10.

其定义类似于:public static final BigInteger ONE = valueOf(1);

BigDecimal:除了以上三个常量外还有8个关于舍入的常量,这里不再赘述,可以去查看API的帮助手册.

顺便说一句,BigDecimal由于舍入模式的存在,使得这个类用起来比BigInteger要复杂.这里不再赘述,具体可以查看API的帮助手册,但是平时对起复杂特性用的不多,所以用的时候查阅API也是可行的.

 

二.声明赋值

BigInteger:BigInteger bi = new BigInteger("100");或:BigInteger bi = BigInteger.valueOf(100);

数组定义与基本类型类似.

BigDecimal:BigDecimal bd = new BigDecimal(100);或:BigDecimal bd = BigDecimal.valueOf(100);

BigDecimal的构造函数比BigInteger多一些,感觉用起来更方便些,比如这样定义就是错误的:BigInteger bi = new BigInteger(100);

顺便说一下,java.util包中的Scanner类实现了nextBigInteger()和nextBigDecimal()方法,可以用来读入控制台输入的BigInteger和BigDecimal.给个例子:

 

Scanner sc = new Scanner(System.in);  while(sc.hasNext()){      BigInteger bi;      //BigDecimal bd;      bi = sc.nextBigInteger();//读入BigInteger      // bd = sc.nextBigDecimal();//读入BigDecimal      System.out.println(bi.toString());      //System.out.println(bd.toString());  }  

 

 

三.相关函数

主要介绍一下四则运算等函数:

用两个例子来说明比较直观一些:

BigInteger:

 
package Factorial;    import java.math.BigInteger;  import java.util.Random;  /**  * 测试BigInteger类的一些函数  * @author LY 2011-10-27  * */  public class BigIntegerDemo {      public static void main(String[] arguments){          System.out.println("构造两个BigInteger对象: ");          //BigInteger(int numBits, Random rnd)           //构造一个随机生成的 BigInteger,它是在 0 到 (2^numBits - 1)(包括)范围内均匀分布的值          BigInteger bi1 =  new BigInteger(55,new Random());          System.out.println("bi1 = " + bi1);                    //BigInteger(byte[] val)           //将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。          BigInteger bi2 = new BigInteger(new byte[]{3,2,3});          System.out.println("bi2 = " + bi2);                    //加          System.out.println("bi1 + bi2 = " + bi1.add(bi2));          //减          System.out.println("bi1 - bi2 = " + bi1.subtract(bi2));          //乘          System.out.println("bi1 * bi2 = " + bi1.multiply(bi2));          //指数运算          System.out.println("bi1的2次方 = " + bi1.pow(2));          //整数商          System.out.println("bi1/bi2的整数商: " + bi1.divide(bi2));          //余数          System.out.println("bi1/bi2的余数: " + bi1.remainder(bi2));          //整数商+余数          System.out.println("bi1 / bi2 = " + bi1.divideAndRemainder(bi2)[0] +                   "--" + bi1.divideAndRemainder(bi2)[1]);          System.out.println("bi1 + bi2 = " + bi1.add(bi2));          //比较大小,也可以用max()和min()          if(bi1.compareTo(bi2) > 0)                   System.out.println("bd1 is greater than bd2");               else if(bi1.compareTo(bi2) == 0)                   System.out.println("bd1 is equal to bd2");               else if(bi1.compareTo(bi2) < 0)                   System.out.println("bd1 is lower than bd2");          //返回相反数          BigInteger bi3 = bi1.negate();          System.out.println("bi1的相反数: " + bi3);          //返回绝对值          System.out.println("bi1的绝对值:  " + bi3.abs());      }    }  

运行结果:

 

构造两个BigInteger对象:   bi1 = 8893838204110884  bi2 = 197123  bi1 + bi2 = 8893838204308007  bi1 - bi2 = 8893838203913761  bi1 * bi2 = 1753180068308949786732  bi1的2次方 = 79100358000902314326836967261456  bi1/bi2的整数商: 45118216565  bi1/bi2的余数: 168389  bi1 / bi2 = 45118216565--168389  bi1 + bi2 = 8893838204308007  bd1 is greater than bd2  bi1的相反数: -8893838204110884  bi1的绝对值:  8893838204110884  

BigDecimal:

 

package Factorial;    import java.math.BigDecimal;;  /**  * 测试BigDecimal类的一些函数  * @author LY 2011-10-27  * */  public class BigDecimalDemo {      public static void main(String[] arguments){          System.out.println("构造两个BigDecimal对象: ");          //用char[]数组创建BigDecimal对象,第二个参数为位移offset,          //第三个参数指定长度          BigDecimal bd1 = new BigDecimal("3464656776868432998434".toCharArray(),2,15);          System.out.println("bd1 = " + bd1);          //用double类型创建BigDecimal对象          BigDecimal bd2 = new BigDecimal(134258767575867.0F);          System.out.println("bd2 = " + bd2);                    //加          System.out.println("bd1 + bd2 = " + bd1.add(bd2));          //减          System.out.println("bd1 - bd2 = " + bd1.subtract(bd2));          //乘          System.out.println("bd1 * bd2 = " + bd1.multiply(bd2));          //指数运算          System.out.println("bd1的2次方 = " + bd1.pow(2));          //取商的整数部分          System.out.println("bd1/bd2的整数商: " + bd1.divideToIntegralValue(bd2));          //返回余数计算为:this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))          //System.out.println(bd1.subtract(bd1.divideToIntegralValue(bd2).multiply(bd2)));          System.out.println("bd1/bd2的余数: " + bd1.remainder(bd2));          //取商和余,即bd1.divideToIntegralValue(bd2)与bd1.remainder(bd2)          System.out.println("bd1 / bd2 = " + bd1.divideAndRemainder(bd2)[0] +                   "--" + bd1.divideAndRemainder(bd2)[1]);          //比较大小,也可以用max()和min()          if(bd1.compareTo(bd2) > 0)                   System.out.println("bd1 is greater than bd2");               else if(bd1.compareTo(bd2) == 0)                   System.out.println("bd1 is equal to bd2");               else if(bd1.compareTo(bd2) < 0)                   System.out.println("bd1 is lower than bd2");          //末位数据精度          System.out.println("bd1的末位数据精度:  " + bd1.ulp());                }    }  

运行结果:

构造两个BigDecimal对象:   bd1 = 646567768684329  bd2 = 134258765070336  bd1 + bd2 = 780826533754665  bd1 - bd2 = 512309003613993  bd1 * bd2 = 86807390157840676971865964544  bd1的2次方 = 418049879501431972683650180241  bd1/bd2的整数商: 4  bd1/bd2的余数: 109532708402985  bd1 / bd2 = 4--109532708402985  bd1 is greater than bd2  bd1的末位数据精度:  1  

四、相关例子

HDU 1002

a+b大数版

import java.math.BigInteger;import java.util.Scanner;public class Main {    void solve () {        BigInteger a, b, c;        Scanner cin = new Scanner(System.in);        int t = cin.nextInt ();        for (int i = 1; i <= t; i++) {            System.out.println ("Case " + i + ":");            a = cin.nextBigInteger ();            b = cin.nextBigInteger ();            System.out.println (a + " + " + b + " = " + a.add (b));            if (i != t) System.out.println ();        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1042


阶乘大数版

import java.math.BigInteger;import java.util.Scanner;public class Main {    int maxn = 10005;    void solve () {        Scanner cin = new Scanner(System.in);        int n;        while (cin.hasNext()) {            n = cin.nextInt ();            BigInteger ans = BigInteger.valueOf (1);            for (int i = 2; i <= n; i++) {                ans = ans.multiply (BigInteger.valueOf (i));            }            System.out.println (ans);        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1250

斐波那契数列大数版

import java.math.BigInteger;import java.util.Scanner;public class Main {    void solve () {        Scanner cin = new Scanner(System.in);        BigInteger f1, f2, f3, f4, ans;        while (cin.hasNext ()) {            int n = cin.nextInt ();            f1 = BigInteger.valueOf (1);            f2 = f3 = f4 = ans = f1;            if (n <= 4) {                System.out.println ("1");                continue;            }            for (int j = 5; j <= n; j++) {                ans = f1.add (f2.add (f3.add (f4)));                f1 = f2;                f2 = f3;                f3 = f4;                f4 = ans;            }            System.out.println (ans);        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1297


f(n)=f(n1)+f(n2)+f(n4)

import java.math.*;import java.util.*;public class Main {    void solve () {        Scanner cin = new Scanner(System.in);        BigInteger[] ans = new BigInteger[1001];        ans[1] = BigInteger.valueOf (1);        ans[2] = BigInteger.valueOf (2);        ans[3] = BigInteger.valueOf (4);        ans[4] = BigInteger.valueOf (7);        for (int i = 5; i <= 1000; i++) {            ans[i] = ans[i-1].add (ans[i-2].add (ans[i-4]));        }        while (cin.hasNext ()) {            int n = cin.nextInt ();            System.out.println (ans[n]);        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1715

斐波那契数列

import java.math.BigInteger;import java.util.Scanner;public class Main {    void solve () {        Scanner cin = new Scanner(System.in);        int t = cin.nextInt ();        BigInteger f1, f2, f3;        for (int i = 0; i < t; i++) {            int n = cin.nextInt ();            f1 = BigInteger.valueOf (1);            f2 = BigInteger.valueOf (1);            f3 = BigInteger.valueOf (0);            if (n == 1 || n == 2) {                System.out.println ("1");                continue;            }            for (int j = 3; j <= n; j++) {                f3 = f1.add (f2);                f1 = f2;                f2 = f3;            }            System.out.println (f3);        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1753

高精度小数,要去掉末尾的后导0.

import java.math.*;import java.util.*;public class Main {    void solve () {        //BigInteger a, b, c;        Scanner cin = new Scanner(System.in);        BigDecimal a = BigDecimal.valueOf (0);        BigDecimal b = BigDecimal.valueOf (0);        while (cin.hasNext ()) {            a = cin.nextBigDecimal ();            b = cin.nextBigDecimal ();            System.out.println (a.add (b).stripTrailingZeros().toPlainString());        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}

HDU 1865

f(n)=f(n1)+f(n2)

import java.math.*;import java.util.*;public class Main {    void solve () {        BigInteger a, b, c;        Scanner cin = new Scanner(System.in);        int t = cin.nextInt ();        String s;        for (int l = 1; l <= t; l++) {            s = cin.next ();            int n = s.length ();            if (n == 1 || n == 2) {                System.out.println (n);                continue;            }            BigInteger f1 = BigInteger.valueOf (1);            BigInteger f2 = BigInteger.valueOf (2);            BigInteger f3 = BigInteger.valueOf (0);            for (int i = 3; i <= n; i++) {                f3 = f1.add (f2);                f1 = f2;                f2 = f3;            }            System.out.println (f3);        }    }    public static void main (String[] args) {        Main work = new Main();        work.solve ();    }}
1 0
原创粉丝点击