一,biginteger ① value.Of(参数); 这个函数的作用是将括号内的参数转换成指定的数据类型
1 2 3 4 5 6 7 8 • int A=42; • BigInteger f=BigInteger.valueOf(A); • System.out.println("f="+f); //输出的f将会等于BigInteger型的42 • // 答案: f=42 • String s="12345"; • BigInteger c=BigInteger.valueOf(s); • // 则c=12345;※※※需要注意的是不重写的话,jdk1.8 版本是无法支持这种转换的
② add()方法; 这个函数的作用是将大整数 加起来
1 2 3 4 • BigInteger c=new BigInteger("6"); • BigInteger d=new BigInteger("3"); • System.out.println("c+d="+c.add(d)); //答案输出: c+d=9
③subtract()方法,这个函数的作用是将大整数相减,例如以下例子,运用时前者减后者
1 2 3 4 • BigInteger c=new BigInteger("5"); • BigInteger d=new BigInteger("3"); • System.out.println("d-c="+d.subtract(c)); //答案输出: d-c=-2
④multiply()方法,这个函数的作用是将大整数相乘
1 2 3 4 • BigInteger c=new BigInteger("6"); • BigInteger d=new BigInteger("3"); • System.out.println("c*d="+c.multiply(d)); //答案输出: c*d=18
⑤divide()方法,这个函数的作用是将大整数做除法
1 2 3 4 • BigInteger c=new BigInteger("6"); • BigInteger d=new BigInteger("4"); • System.out.println("c/d="+c.divide(d)); // 答案输出;c/d=1
⑥remainder()方法,这个函数的作用是将大整数取余
⑦pow(exponent)方法,这个函数的作用是将大整数取exponent的指数,例如a.pow(b)==a^b;
⑧gcd()方法,这个函数的作用是将两个大整数取最大公约数,例如a.gcd(b);
⑨abs()方法,这个函数的作用是取绝对值,例如
1 2 3 • BigInteger c=new BigInteger("-9"); • System.out.println(c.abs()); //答案输出: 9
⑩negate()方法,这个函数的作用是取数的相反数,例如
1 2 3 • BigInteger c=new BigInteger("9"); • System.out.println(c.negate()); // 答案输出: -9
⑪mod()方法; 这个函数的作用是对数进行取余 a.mod(b)=a%b=a.remainder(b);
⑫max()方法,min()方法,分别是比较两个数的大小,例如a.max(b); 取a,b中的最大值
⑬compareTo()方法这个方法是用来比较两个大整数大小的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 • public void testCompare() { • BigInteger bigNum1 = new BigInteger("52"); • BigInteger bigNum2 = new BigInteger("27"); • //1.compareTo():返回一个int型数据(1 大于; 0 等于; -1 小于) • int num = bigNum1.compareTo(bigNum2); //1 • //2.max():直接返回大的那个数,类型为BigInteger • // 原理:return (compareTo(val) > 0 ? this : val); • BigInteger compareMax = bigNum1.max(bigNum2); //52 • //3.min():直接返回小的那个数,类型为BigInteger • // 原理:return (compareTo(val) < 0 ? this : val); • BigInteger compareMin = bigNum1.min(bigNum2); //27 •}
⑭equals()方法,判断两个大整数是否相等,例如c.equals(d) 相等就返回 true;
BigInteger一些基本类型的转换: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public void testToAnother() { BigInteger bigNum = new BigInteger("38"); int radix = 2; //1.转换为bigNum的二进制补码形式 byte[] num1 = bigNum.toByteArray(); //2.转换为bigNum的十进制字符串形式 String num2 = bigNum.toString(); //38 //3.转换为bigNum的radix进制字符串形式 String num3 = bigNum.toString(radix); //100110 //4.将bigNum转换为int int num4 = bigNum.intValue(); //5.将bigNum转换为long long num5 = bigNum.longValue(); //6.将bigNum转换为float float num6 = bigNum.floatValue(); //7.将bigNum转换为double double num7 = bigNum.doubleValue(); }
二,BigDecimal 用来对超过16位有效位的数进行精确的运算。
构造方法 1 2 3 4 //int,string转BigDecimal BigDecimal a = new BigDecimal(10); //long,float,double转BigDecimal BigDecimal b = BigDecimal.valueOf(5.55);
常用方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 //加法 BigDecimal c = a.add(b); //减法 c = a.subtract(b); //乘法 c = a.multiply(b); c = a.multiply(b).setScale(4,BigDecimal.ROUND_HALF_UP); //除法 c = a.divide(b, 2, RoundingMode.HALF_UP); //求余数 c = a.remainder(b); //最小值 c = a.min(b); //最大值 c = a.max(b); //绝对值 c = a.abs(); //相反数 c = a.negate(); //去除多余的0 a.stripTrailingZeros(); //去除多余的0, 并转为字符串 //不能使用toString(), 会返回科学计数法 String a_string = a.stripTrailingZeros().toPlainString(); //返回此BigDecimal的正负, -1为负, 0为0 , 1为正 int a_int = a.signum(); //比较大小, -1为a<b, 0为a=b , 1为a>b a_int = a.compareTo(b); //BigDecimal转其他类型 String b_string = b.toString(); int b_int = b.intValue(); long b_long = b.longValue(); float b_float = b.floatValue(); double b_double = b.doubleValue();
精度设置 1 a.setScale(4,BigDecimal.ROUND_HALF_UP);
BigDecimal.ROUND_UP 远离零的舍入模式
BigDecimal.ROUND_DOWN 接近零的舍入模式
BigDecimal.ROUND_CEILING 接近正无穷大的舍入模式
BigDecimal.ROUND_FLOOR 接近负无穷大的舍入模式
BigDecimal.ROUND_HALF_UP 向最近的一边数字舍入, 四舍五入 这个是最常用的
BigDecimal.ROUND_HALF_DOWN 向最近的一边数字舍入, 五舍六入
BigDecimal.ROUND_HALF_EVEN 向最近的一边数字舍入,四舍六入. 为五时,如果最近的一边数字是奇数,入,如果是偶数,舍
BigDecimal.ROUND_UNNECESSARY 计算结果是精确的,不需要舍入 结果不精确会抛出异常ArithmeticException
三,题目 题目描述 本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
你一定听说过这个故事。国王对发明国际象棋的大臣很佩服,问他要什么报酬,大臣说:请在第 11 个棋盘格放 11 粒麦子,在第 22 个棋盘格放 22 粒麦子,在第 33 个棋盘格放 44 粒麦子,在第 44 个棋盘格放 88 粒麦子,……后一格的数字是前一格的两倍,直到放完所有棋盘格(国际象棋共有 6464 格)。
国王以为他只是想要一袋麦子而已,哈哈大笑。
当时的条件下无法准确计算,但估算结果令人吃惊:即使全世界都铺满麦子也不够用!
请你借助计算机准确地计算,到底需要多少粒麦子。
运行限制
程序代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 1; int count = 1; BigInteger a = BigInteger.valueOf(n); BigInteger b = BigInteger.valueOf(2); BigInteger sum = BigInteger.valueOf(1); // 这样说明初始化第一个格子已经放了 while (count < 64) { a = a.multiply(b); sum = sum.add(a); count++; } System.out.println(sum); } }