java大数知识点

it2022-05-05  113

介绍

java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表大浮点型。因为这两种类的使用方法是一样的且通常情况下我们处理的数据是整数,所以下面我们以BigInteger为例进行讲解。

头文件: import java.util.*;

import java.math.*; (或math.BigInteger)

基本用法

1、新建一个大整数对象

//第一种,参数是字符串 BigInteger a=new BigInteger(123); //第二种,输入大数 Scanner sc = new Scanner(System.in); BigInteger a = sc.nextBigInteger();

2、大整数的四则运算

a. add(b); //a,b均为BigInteger类型,加法 a.subtract(b); //减 法 a.divide(b); //除法 a.multiply(b); //乘法

3、大整数比较大小

a.equals(b); //如果a、b相等返回true否则返回false a.compareTo(b); //a小于b返回-1,等于返回0,大于返回1

4、常用方法

a.mod(b); //求余 a.gcd(b); //求最大公约数

5、求大整数的位数

//先转换成字符串再求字符串的长度 a.toString().length(); //a的类型为BigInteger

输入框架

1.循环输入遇EOF结束

Scanner sc = new Scanner(System.in); //相当于C++中的cin,只不过这个cin需要自己创建 while (sc.hasNext()){ //等同于!=EOF BigInteger a; a = sc.nextBigInteger(); //读入一个BigInteger; System.out.println(a); //输出a并换行 }

2.输入一个整数T,T组测试样例

Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-- > 0) { System.out.println(T); }
tip:

1、不能有包名,也就是说我们要把主类放到默认的包里,如果你的代码里出现形如package cn.gov.test;这样的代码你很有可能会收获到RE 2、提交的类的类名必须为Main,如果是其他的名字你有可能收获到CE也有可能收获到WA(例如UVA) 3、不要想当然的认为执行了a.add(b)之后a的值会发生改变,这句代码只是求值而已,相当于a + b,计算了a + b之后a的值会改变吗?当然不会!所以我们要想达到 a = a + b的效果需要写a = a.add(b)

例题:

1.A+B: 我有一个非常简单的问题。给定两个整数A和B,你的工作是计算A + B的和。 输入 输入的第一行包含整数T(1 <= T <= 20),表示测试用例的数量。然后是T行, 每行包含两个正整数,A和B.请注意,整数非常大,这意味着您不应该使用32位整数 来处理它们。您可以假设每个整数的长度不超过1000。 对于每个测试用例,您应输出两行。第一行是“Case#:”,#表示测试用例的编号。 第二行是方程“A + B = Sum”,Sum表示A + B的结果。注意方程中有一些空格。 在两个测试用例之间输出一个空行。 Sample Input

2 1 2 112233445566778899 998877665544332211

Sample Output

Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110 import java.math.BigInteger; import java.util.Scanner; public class Main{ //java中main函数必须为static静态 public static void main(String[] args){ //Scanner 类作为输入 Scanner sc = new Scanner(System.in); //nextInt输入一个整形 int n = sc.nextInt(); for (int i = 1; i <= n; i++) { //nextBigInteger输入一个大数型 BigInteger a = sc.nextBigInteger(); BigInteger b = sc.nextBigInteger(); //println表示输出并换行 System.out.println("Case "+i+":"); //()各部分用+连接起来 System.out.println(a+" + "+b+" = " +a.add(b)); if (i != n) { //单纯换行 System.out.println(); } } } }

2.大数分数比较 Bobo有两个分数 x/a、y/b,他想比较一下

输入描述: 输入由几个测试用例组成,并以文件结尾结束。 每个测试用例包含四个整数x,a,y,b。 0≤x,y≤1018,1≤a,b≤109 最多10^5测试用例。

输出描述: 对于每个测试用例, 打印"=" if x/a=y/b。 打印"<" if x/a<y/b。 否则打印">"。

示例1 输入

1 2 1 1 1 1 1 2 1 1 1 1

输出

< > = import java.util.Scanner; import java.math.BigInteger; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); BigInteger x, a, y, b, ans1, ans2; //EOF结束表示 while (sc.hasNext()) { x = sc.nextBigInteger(); a = sc.nextBigInteger(); y = sc.nextBigInteger(); b = sc.nextBigInteger(); ans1 = x.divide(b); ans2 = a.divide(y); //大数类compareTo:相等返回0,ans1>ans2返回1.否则-1 int ans = ans1.compareTo(ans2); if (ans == 0) System.out.println("="); else if (ans == 1) System.out.println(">"); else System.out.println("<"); } } }

3.大数阶乘 给定一个整数N(0≤N≤10000),你的任务是计算N!

输入 一行中有一个N,处理到文件末尾。 样本输入

1 2 3

样本输出

1 2 6 //数据较小可先打表 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger f[] = new BigInteger[5500]; f[0] = f[1] = BigInteger.ONE; for (int i = 2; i <= 5000; ++i) { f[i] = f[i - 1].multiply(BigInteger.valueOf(i)); } Scanner cin = new Scanner(System.in); while (cin.hasNext()) { int m = cin.nextInt(); System.out.println(f[m]); } } }

4.大菲波数 题目描述:计算第n项Fibonacci数值,n可能比较大 分析:这个题与上一题一样,需要先打表在输出

import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); BigInteger[] nums = new BigInteger[1010]; nums[1] = new BigInteger("1"); nums[2] = new BigInteger("1"); for(int i = 3; i <= 1000; i++) nums[i] = nums[i - 1].add(nums[i - 2]); int T = cin.nextInt(); while (T > 0) { T--; int n = cin.nextInt(); System.out.println(nums[n]); } } }

最新回复(0)