标签:大小 scan strip [] lin tip equal rgs for
碰到很多大数类的题目,听说JAVA比较流氓,赶紧去学习一波( <(?????)> )
大数相加:
1 //JAVA 大数相加 2 import java.math.BigInteger; 3 import java.util.*; 4 import java.io.*; 5 6 public class Main 7 { 8 public static void main(String args[]) 9 { 10 Scanner cin = new Scanner(System.in); 11 BigInteger a = cin.nextBigInteger(); 12 BigInteger b = cin.nextBigInteger(); 13 BigInteger ans = a.add(b); 14 System.out.println(ans); 15 } 16 }
大数相乘:
1 //JAVA 大数相乘 2 import java.math.BigInteger; 3 import java.util.*; 4 import java.io.*; 5 6 public class Main 7 { 8 public static void main(String args[]) 9 { 10 Scanner cin = new Scanner(System.in); 11 BigInteger a = cin.nextBigInteger(); 12 BigInteger b = cin.nextBigInteger(); 13 BigInteger ans = a.multiply(b); 14 System.out.println(ans); 15 } 16 }
大数相减:public BigInteger subtract(BigInteger val)
大数相除:public BigInteger divide(BigInteger val)
大数阶乘:
1 //JAVA 大数阶乘 2 import java.io.*; 3 import java.math.BigInteger; 4 import java.util.*; 5 6 public class Main 7 { 8 public static void main(String args[]) 9 { 10 Scanner cin = new Scanner(System.in); 11 int n = cin.nextInt(); //输入int型 12 BigInteger ans = BigInteger.ONE; 13 for(int i = 1; i <= n; i++) 14 ans = ans.multiply(BigInteger.valueOf(i)); 15 System.out.println(ans); 16 } 17 }
大数比较大小
1 //JAVA 大数比较大小 2 import java.io.*; 3 import java.math.BigInteger; 4 import java.util.*; 5 6 public class Main 7 { 8 public static void main(String args[]) 9 { 10 Scanner cin = new Scanner(System.in); 11 while(cin.hasNext()) 12 { 13 BigInteger a = cin.nextBigInteger(); 14 BigInteger b = cin.nextBigInteger(); 15 if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break; 16 int flag = a.compareTo(b); 17 if(flag == -1) 18 System.out.println("a<b"); 19 else if(flag == 0) 20 System.out.println("a==b"); 21 else 22 System.out.println("a>b"); 23 } 24 } 25 }
高精度幂
1 //JAVA 高精度幂 2 import java.io.*; 3 import java.math.BigDecimal; 4 import java.util.*; 5 6 public class Main 7 { 8 public static void main(String args[]) 9 { 10 Scanner cin = new Scanner(System.in); 11 while(cin.hasNext()) 12 { 13 BigDecimal ans = cin.nextBigDecimal(); 14 int n = cin.nextInt(); 15 String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉后面的0和小数点 16 if(res.startsWith("0")) //去掉前导0 17 { 18 res = res.substring(1); 19 } 20 System.out.println(res); 21 } 22 } 23 }
//这里的BigDecimal相当于double,但是精度更高
标签:大小 scan strip [] lin tip equal rgs for
原文地址:http://www.cnblogs.com/Leonard-/p/7636639.html