Java大数处理

来源:互联网 发布:java zxing生成二维码 编辑:程序博客网 时间:2024/05/06 11:06

Ⅰ基本函数:

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

   比如 int a=3;

 

      

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. BigInteger b=BigInteger.valueOf(a);  


 

 

     则b=3;

         String s=”12345”;

 

      

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. BigInteger c=BigInteger.valueOf(s);  


 

 

       则c=12345;

 

2.add(); 大整数相加

  

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. BigInteger a=new BigInteger(“23”);  
  2.   
  3.    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.punlic 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

Ⅲ.基本操作

1.   读入:

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

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. Scanner cin=new Scanner(System.in);// 读入  
  2.   
  3. while(cin.hasNext())   //等同于!=EOF  
  4.   
  5. {  
  6.   
  7.    int n;  
  8.   
  9.    BigInteger m;  
  10.   
  11.    n=cin.nextInt(); //读入一个int;  
  12.   
  13.    m=cin.BigInteger();//读入一个BigInteger;  
  14.   
  15. System.out.print(m.toString());  
  16.   
  17. }  


 

 

Ⅳ.运用

四则预算:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.util.Scanner;  
  2. import java.math.*;  
  3. import java.text.*;  
  4.   
  5. public class Main   
  6. {  
  7. public static void main(String args[])  
  8. {  
  9.    Scanner cin = new Scanner ( System.in );  
  10.    BigInteger a,b;  
  11.    int c;  
  12.    char op;  
  13.    String s;  
  14.     
  15.    while( cin.hasNext() )  
  16.    {  
  17.     a = cin.nextBigInteger();  
  18.     s = cin.next();  
  19.     op = s.charAt(0);  
  20.     if( op == '+')  
  21.     {  
  22.      b = cin.nextBigInteger();  
  23.      System.out.println(a.add(b));  
  24.     }  
  25.     else if( op == '-')  
  26.     {  
  27.      b = cin.nextBigInteger();  
  28.      System.out.println(a.subtract(b));  
  29.     }  
  30.     else if( op == '*')  
  31.     {  
  32.      b = cin.nextBigInteger();  
  33.      System.out.println(a.multiply(b));  
  34.     }  
  35.     else  
  36.     {  
  37.      BigDecimal a1,b1,eps;  
  38.      String s1,s2,temp;  
  39.      s1 = a.toString();  
  40.        a1 = new BigDecimal(s1);  
  41.      b = cin.nextBigInteger();  
  42.      s2 = b.toString();  
  43.      b1 = new BigDecimal(s2);  
  44.      c = cin.nextInt();  
  45.      eps = a1.divide(b1,c,4);  
  46.      //System.out.println(a + " " + b + " " + c);  
  47.      //System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);  
  48.      System.out.print( a.divide(b) + " " + a.mod(b) + " ");  
  49.      if( c != 0)  
  50.      {  
  51.       temp = "0.";  
  52.       for(int i = 0; i < c; i ++) temp += "0";  
  53.       DecimalFormat gd = new DecimalFormat(temp);  
  54.       System.out.println(gd.format(eps));  
  55.      }  
  56.      else System.out.println(eps);  
  57.     }  
  58.    }  
  59.    }  
  60. }  
0 0