java大数类在acm中的应用

java提交样例:

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);
        int t;
        t = cin.nextInt();//读取int
        while(t--){
            //doing something
        }
        /*
        while(cin.hasNext()){
            //doing something
        }
        */
    }
}

读取的部分

BigInteger a;
a = cin.nextInteger();
String s;
s = cin.next();
char c = s.charAt(0);
Long l;
l = cin.nextLong();

大数类的初始化:

BigInteger a = a.BigInteger.valueOf(int or string);
// 123 或者 "1235" 都可以
BigInteger a1 = new BigInteger("1")

大数类的函数:

a = a.add(BigInteger.valueOf(1));// a = a + 1;
a = a.subtract(BigInteger.valueOf(1));// a = a - 1;
a = a.multiply(BigInteger.valueOf(2));// a = a*2;
a = a.divide(BigInteger.valueOf(2));// a = a/2;
a = a.pow(23);// a = a^23;
//注意,只有pow的里边填的是int 其他的都得是BigInteger
BigInteger a1 = new BigInteger("1");
BigInteger a2 = new BigInteger("2");
a1.compareTo(a2);//大于返回1 等于0 小于-1
if( a1.equals(BigInteger.ZERO) ){
    //BigInteger.ZERO也可以换成BigInteger.valueOf(xxx);
}

BigDecimal

BigDecimal b = BigDecimal.valueOf(double);
b = b.stripTrailingZeros();//去掉末尾无意义的0
String s = b.toPlainString();//不带指数段 消去科学记数法
//s = s.substring(1); //s = "123456" -> s = "23456"

练习题:

大数阶乘:
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28

棋盘覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45

比较大小
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73

大数加法
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103

递推求值
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114

高精度幂
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=155

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值