JAVA大数类练手

JAVA大数类练手

链接:http://blog.csdn.net/niushuai666/article/details/6972991

今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识。果断水了6道题。。。。。。都是非常基础的。就当的练手的吧。

学到的只是一些大数类的基本操作。以后多做点这样的题,争取熟练运用水大数题。。。大笑


大数阶乘

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

代码如下:

  1. import java.io.*;  
  2. import java.math.BigInteger;  
  3. import java.util.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner cin = new Scanner(System.in);     
  10.         int n = cin.nextInt();  
  11.         BigInteger ans = BigInteger.ONE;  
  12.         for(int i = 1; i <= n; ++i)  
  13.             ans = ans.multiply(BigInteger.valueOf(i));  
  14.         System.out.println(ans);  
  15.     }  
  16. }  
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
	public static void main(String args[])
	{
		Scanner cin = new Scanner(System.in);	
		int n = cin.nextInt();
		BigInteger ans = BigInteger.ONE;
		for(int i = 1; i <= n; ++i)
			ans = ans.multiply(BigInteger.valueOf(i));
		System.out.println(ans);
	}
}


棋盘覆盖

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

代码如下:

  1. import java.math.BigInteger;  
  2. import java.util.*;  
  3. import java.io.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner in = new Scanner(System.in);  
  10.         int test = in.nextInt();  
  11.         while(test-- > 0)  
  12.         {  
  13.             int n;  
  14.             n = in.nextInt();  
  15.             BigInteger a = new BigInteger("4");  
  16.             for(int i = 1; i < n; ++i)  
  17.                 a = a.multiply(BigInteger.valueOf(4));  
  18.             System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));  
  19.         }  
  20.     }  
  21. }  
import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);
		int test = in.nextInt();
		while(test-- > 0)
		{
			int n;
			n = in.nextInt();
			BigInteger a = new BigInteger("4");
			for(int i = 1; i < n; ++i)
				a = a.multiply(BigInteger.valueOf(4));
			System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));
		}
	}
}

比较大小

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

代码如下:

  1. import java.io.*;  
  2. import java.math.BigInteger;  
  3. import java.util.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner cin = new Scanner(System.in);     
  10.         while(cin.hasNext())  
  11.         {  
  12.             BigInteger a = cin.nextBigInteger();  
  13.             BigInteger b = cin.nextBigInteger();  
  14.             if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))  
  15.                 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. }  
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
	public static void main(String args[])
	{
		Scanner cin = new Scanner(System.in);	
		while(cin.hasNext())
		{
			BigInteger a = cin.nextBigInteger();
			BigInteger b = cin.nextBigInteger();
			if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))
				break;
			int flag = a.compareTo(b);
			if(flag == -1)
				System.out.println("a<b");
			else if(flag == 0)
				System.out.println("a==b");
			else
				System.out.println("a>b");
		}
	}
}


大数加法

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

代码如下:

  1. import java.math.BigInteger;  
  2. import java.util.*;  
  3. import java.io.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner in = new Scanner(System.in);  
  10.         int n = in.nextInt();         
  11.         for(int i = 1; i <= n; ++i)  
  12.         {  
  13.             BigInteger a = in.nextBigInteger();  
  14.             BigInteger b = in.nextBigInteger();  
  15.             BigInteger ans = a.add(b);  
  16.             System.out.println("Case " + i + ":");  
  17.             System.out.println(a + " + " + b + " = " +ans);  
  18.         }  
  19.     }  
  20. }  
import java.math.BigInteger;
import java.util.*;
import java.io.*;

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


递推求值

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

代码如下:

  1. import java.io.*;  
  2. import java.math.BigInteger;  
  3. import java.util.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner cin = new Scanner(System.in);     
  10.         BigInteger a[] = new BigInteger[100];  
  11.         while(cin.hasNext())  
  12.         {  
  13.             for(int i = 0; i <= 2; ++i)  
  14.                 a[i] = cin.nextBigInteger();  
  15.             for(int i = 3; i <= 99; ++i)  
  16.                 a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);  
  17.             System.out.println(a[99]);  
  18.         }  
  19.     }  
  20. }  
import java.io.*;
import java.math.BigInteger;
import java.util.*;

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


高精度幂

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

代码如下:

  1. import java.io.*;  
  2. import java.math.BigDecimal;  
  3. import java.util.*;  
  4.   
  5. public class Main  
  6. {  
  7.     public static void main(String args[])  
  8.     {  
  9.         Scanner cin = new Scanner(System.in);     
  10.         while(cin.hasNext())  
  11.         {  
  12.             BigDecimal ans = cin.nextBigDecimal();  
  13.             int n = cin.nextInt();  
  14.             String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0   
  15.             if(res.startsWith("0")) //去掉前导0   
  16.             {  
  17.                 res = res.substring(1);  
  18.             }  
  19.             System.out.println(res);  
  20.         }  
  21.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值