Java大数练习
大数阶乘
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 //import java.text.*; 5 6 public class Main { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) throws Exception{ 12 // TODO Auto-generated method stub 13 Scanner cin = new Scanner (System.in); 14 PrintWriter cout = new PrintWriter(System.out); 15 16 int n = cin.nextInt(); 17 BigInteger ans = BigInteger.ONE; 18 for(int i=1;i<=n;i++) 19 ans = ans.multiply(BigInteger.valueOf(i)); 20 cout.println(ans); 21 cout.flush(); 22 } 23 24 }
比较大小
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 import java.text.*; 5 6 public class Main { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 Scanner cin = new Scanner(System.in); 14 while(true) 15 { 16 BigInteger a = cin.nextBigInteger(); 17 BigInteger b = cin.nextBigInteger(); 18 if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO) ) 19 { 20 break; 21 } 22 int flag = a.compareTo(b); 23 if(flag == 0) 24 System.out.println("a==b"); 25 else if(flag == -1) 26 System.out.println("a<b"); 27 else 28 System.out.println("a>b"); 29 } 30 } 31 32 }
大数加法
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 6 public class Main { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 Scanner cin = new Scanner(System.in); 14 int T = cin.nextInt(); 15 int cas = 1; 16 for(int i=1;i<=T;i++) 17 { 18 BigInteger a = cin.nextBigInteger(); 19 BigInteger b = cin.nextBigInteger(); 20 BigInteger ans = a.add(b); 21 22 System.out.println("Case "+i+":"); 23 System.out.println(a+" + "+b+" = "+ans); 24 } 25 } 26 27 }