给大家推荐个靠谱的公众号程序员探索之路,大家一起加油
JAVA的两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,理论上能够表示无限大的数。
Java还是要好好学的!!!!!!!
代码试列:
package com.xujin;
import java.util.*;
import java.math.*;
public class Test {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
//BigInteger类型的常量
BigInteger A = BigInteger.ONE;
System.out.println("BigInteger.ONE的结果为 " + A);//1
BigInteger B = BigInteger.TEN;
System.out.println("BigInteger.TEN的结果为 " + B);//10
BigInteger C = BigInteger.ZERO;
System.out.println("BigInteger.ZERO的结果为 " + C);//0
//初始化
BigInteger c = new BigInteger("12345670",8);//c = 01234567890 ,八进制
System.out.println(c);//2739128
BigInteger d = BigInteger.valueOf(100);//d = 100
BigInteger e = new BigInteger(new byte[]{1,0});//00000001 00000000
System.out.println(e);//256
System.out.println(e.bitCount());
System.out.println(e.bitLength());
//运算
System.out.println("请输入大整数a,b");
while (cin.hasNext()) {//等同于!=EOF
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger c1 = a.add(b); // 大数加法
System.out.println("加的结果为 " + c1);
BigInteger c2 = a.subtract(b); // 大数减法
System.out.println("减的结果为 " + c2);
BigInteger c3 = a.multiply(b); // 大数乘法
System.out.println("乘的结果为 " + c3);
BigInteger c4 = a.divide(b); // 大数除法
System.out.println("除的结果为 " + c4);
BigInteger c5 = a.mod(b);
System.out.println("模的结果为 " + c5);
BigInteger cc5 = a.remainder(b);
System.out.println("余的结果为 " + cc5);
BigInteger c6 = a.max(b);// 取最大
System.out.println("最大为 " + c6);
BigInteger c7 = a.min(b); // 取最小
System.out.println("最小为 " + c7);
BigInteger c8 = a.pow(10); //指数运算
System.out.println("指数运算结果为" + c8);
if (a.equals(b)) // 判断是否相等
System.out.println("相等");
else
System.out.println("不相等");
BigInteger c10 = a.abs(); // 求绝对值
System.out.println("a的绝对值为 " + c10);
BigInteger c11 = a.negate(); // 求相反数
System.out.println("a的相反数为 " + c11);
}
}
}