java 大数类使用及案例

16年9月22日,感觉是时候复习算法,重新进军进军竞赛大军了。所有将会每天进行算法训练,但是不会放弃java软件设计和前端学习。
SO

正题来了,将昨天的大数类整理如下:

  1. 定义大数变量:BigInteger x, BigInteger y;
  2. 大数相加:x.add(y)
  3. 大数相减:x.subtract(y)
  4. 大数相乘:x.multiply(y)
  5. 大数相除:x.divide(y)
  6. 大数模运算:x.remainder(y)
  7. 大数与大数之间赋值:x = y;
  8. 大数与非大数之间赋值:x = BigIntteger.valueOf(int val)
  9. 大数之间大小比较:x.compareTo(y)
更多函数请参考API

Example:

nyoj 45

import java.math.BigInteger;
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        BigInteger res;
        int n, k;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();

        for (int i = 0; i < n; i++)
        {
            res = BigInteger.valueOf(2);
            k  = in.nextInt();
            res = res.pow(2*k);
            res = res.subtract(BigInteger.ONE);
            res = res.divide(BigInteger.valueOf(3));
            System.out.println(res);
        }
    }
}

nyoj 28

import java.math.BigInteger;
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigInteger x, y;
        int n;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        long start = System.currentTimeMillis();
        x = BigInteger.valueOf(1);
        for(int i = 1; i <= n; i++)
        {
            y =  BigInteger.valueOf(i);
            x = x.multiply(y);
        }
        System.out.println(x);
        long end = System.currentTimeMillis();
//      Float time = (float) ((end-start)/1000);
        long runTime = end - start;
        System.out.println(runTime);

    }


}

nyoj 73

import java.math.BigInteger;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        BigInteger x, y;
        Scanner in = new Scanner(System.in);
        x = in.nextBigInteger();
        y = in.nextBigInteger();
        while(!x.equals(BigInteger.ZERO) && !y.equals(BigInteger.ZERO))
        {
            if(x.compareTo(y) > 0) System.out.println("a>b");
            else if(x.compareTo(y) < 0) System.out.println("a<b");
            else System.out.println("a==b");
            x = in.nextBigInteger();
            y = in.nextBigInteger();

        }
    }
}

nyoj 103

import java.math.BigInteger;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        int t;
        BigInteger x, y, res;
        Scanner in = new Scanner(System.in);
        t = in.nextInt();
        for(int i = 1; i <= t; i++)
        {
            x = in.nextBigInteger();
            y = in.nextBigInteger();
            res = x.add(y);
            System.out.println("Case "+i+":");
            System.out.println(x + " + " + y + " = " +res);

        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值