JAVA之大数处理

Ⅰ基本函数:
整数的运算   BigInteger

小数的运算   BigDecimal

 导入类:
java 大数相除不能整除时会抛出异常,解决办法,设置精度;参见:java之BigDecimal

 num1 = num1.divide(num2,10,BigDecimal.ROUND_HALF_EVEN);
    System.out.println(num1);
算阶乘时也可用;

需要转化一下

1 int n = cin.nextInt() ;
2         BigInteger m = BigInteger.valueOf(1) ;
3         for(int i = 1;i<=n;i++){
4             m = m.multiply(BigInteger.valueOf(i)) ;//int 整形转化为 BigInteger  进行计算
5         }
6         System.out.println(m);


import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal
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.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.*包中
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";
DecimalFormat gd = new DecimalFormat(temp);
System.out.println(gd.format(eps));
}
else System.out.println(eps);
}
}
}
}

//输入两个大数进行加法运算  add() 想加

import java.util.Scanner;
import java.math.BigInteger;
public class dashu {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        BigInteger a,b;
            a=input.nextBigInteger();
            b=input.nextBigInteger();
            System.out.println(a.add(b));
        }
    }


//输入两个大数相减  subtract ()  相减
import java.util.*;
import java.math.BigInteger;
public class dashu {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        BigInteger num1 = input.nextBigInteger();
        BigInteger num2 = input.nextBigInteger();
        System.out.println(num1.subtract(num2));
    }
}


//输入两个大数相乘  mulitiply () 相乘
import java.util.* ;
import java.math.BigInteger ;
public class dashu {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        BigInteger m,n;
        m=input.nextBigInteger();
        n=input.nextBigInteger();
            m = m.multiply(n) ;
        System.out.println(m);
    }
}


//两个大数相除 divide()  相除   remainder()  余数
import java.util.*;
import java.math.BigInteger;
public class dashu {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        BigInteger num1 = input.nextBigInteger();
        BigInteger num2 = input.nextBigInteger();
        System.out.println(num1.divide(num2));//相除
        System.out.println(num1.remainder(num2));//余数
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值