Java大数类应用

参考:  飘过的小牛“Java大数类练手”   http://blog.csdn.net/niushuai666/article/details/6972991


1、nyoj-28 大数阶乘  (大数乘)

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=28

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		long num = cin.nextLong();
		BigInteger ans = new BigInteger("1");
		while(num > 0){
			ans = ans.multiply(BigInteger.valueOf(num));
			num -= 1;	
		}
		System.out.println(ans);
		cin.close();
	}
}

2、nyoj-45  棋盘覆盖 (大数幂, 大数减, 大数除)

题目链接: http://acm.nyist.net/JudgeOnline/status.php?pid=45

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int loop = cin.nextInt();
		while(loop-- > 0){
			int k = cin.nextInt();
			BigInteger ans = new BigInteger("4");
			ans = ans.pow(k).subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3));
			System.out.println(ans);
		}
	}
}


3、nyoj-73 比大小 (大数比较)

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=73

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		BigInteger a, b;
		while(true){
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			if(a.equals(BigInteger.valueOf(0)) && b.equals(BigInteger.valueOf(0))){
				break;				
			}
			
			int ans = a.compareTo(b);
			if(ans == 0){
				System.out.println("a==b");				
			}
			else if(ans < 0){
				System.out.println("ab");				
			}	
		}
	}
}

3、nyoj-103 A+B Problem II (大数加法)

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=103

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		BigInteger a, b, ans;
		int loop = cin.nextInt(), i = 0;
		while(i ++ < loop){
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			ans = a.add(b);
			System.out.println("Case " + i + ":");
			System.out.println(a + " + " + b + " = " + ans);
		}
	}
}


4、nyoj-114  某种序列(递推求值)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		BigInteger ans[] = new BigInteger[100];
		while(cin.hasNext()){
			for(int i = 0; i < 3; i ++){
				ans[i] = cin.nextBigInteger();		
			}
			for(int i = 3; i < 100; i ++){
				ans[i] = ans[i - 1].add(ans[i - 2].add(ans[i - 3]));		
			}
			System.out.println(ans[99]);
		}
	}
}

5、nyoj-155 高精度幂

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=155

import java.math.BigDecimal;
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		BigDecimal a;
		int b;
		while(cin.hasNext()){
			a = cin.nextBigDecimal();
			b = cin.nextInt();
			String ans = a.pow(b).stripTrailingZeros().toPlainString();		// 整数去掉小数点和后面的0
			if(ans.startsWith("0")){ 			//去掉前导0   
                ans = ans.substring(1);  
            }  
			System.out.println(ans);
		}
	}
}



你可能感兴趣的:(Java,数论)