频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
JAVA大数类练手
2014-03-19 13:17:18         来源:JAVA大数类练手  
收藏   我要投稿

刚学了点JAVA大数类,找几个大数的题目先练练手。

 

大数阶乘

 

import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
public class Main
{
	public static void main(String[] args)
	{
		Scanner cin = new Scanner (System.in);
		while(cin.hasNext())
		{
			int m = cin.nextInt();
			BigInteger sum = BigInteger.ONE;
			for(int i = 2; i <= m; i++)
			{
				sum = sum.multiply(BigInteger.valueOf(i));
			}
			System.out.println(sum);
		}
		
	}
}

 

https://acm.nyist.net/JudgeOnline/problem.php?pid=45

 

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

public class Main{
	public static void main(String[] args)
	{
		int test;
		Scanner cin = new Scanner (System.in);
		test = cin.nextInt();
		while(test-- > 0)  //搞不懂为啥不加 >0 就编译错误
		{
			BigInteger sum = BigInteger.ONE;
			int n = cin.nextInt();
			for(int i = 1; i <= n; i++)
			{
				sum = sum.multiply(BigInteger.valueOf(4));
			}
			System.out.println(
			sum.subtract(BigInteger.valueOf(1)).pide(BigInteger.valueOf(3)) );
		}
	}
}

https://acm.nyist.net/JudgeOnline/problem.php?pid=73

大数比较

 

import java.math.BigInteger;
import java.util.*;
import java.io.*;
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 f = a.compareTo(b);
			if(f == -1)
				System.out.println(ab);
		}
	}
	
}

https://acm.nyist.net/JudgeOnline/problem.php?pid=103

大数加法

 

import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String[] args)
	{
		int test;
		Scanner cin = new Scanner (System.in);
		test = cin.nextInt();
		for(int item = 1; item <= test; item++)
		{
			BigInteger a = cin.nextBigInteger();
			BigInteger b = cin.nextBigInteger();
			System.out.println(Case  + item + :);
			System.out.println(a+  +  + b +  =  + a.add(b)); // 输出格式
		}
	}
	
}

https://acm.nyist.net/JudgeOnline/problem.php?pid=114

 

递推

 

import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String[] args)
	{
		Scanner cin = new Scanner (System.in);
		while(cin.hasNext())
		{
			BigInteger [] a = new BigInteger [101];
			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]);
		}
	}
	
}

https://acm.nyist.net/JudgeOnline/problem.php?pid=155

 

高精度求幂

 

import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String[] args)
	{
		Scanner cin = new Scanner (System.in);
		while(cin.hasNext())
		{
			BigDecimal ans = cin.nextBigDecimal();
			int n = cin.nextInt();
			ans = ans.pow(n); //求幂
			ans = ans.stripTrailingZeros();//整数去掉小数点和后面的零
			String res = ans.toPlainString();//科学计数法
			if(res.startsWith(0))//去掉前导0
			{
				res = res.substring(1);
			}
			System.out.println(res);
		}
	}
	
}


 

 

 

 

 

 

点击复制链接 与好友分享!回本站首页
相关TAG标签 大数
上一篇:小程序:Java下载单页HTML(可下载引用资源)
下一篇:JAVA EE 项目常用知识之 重写和重载 ArrayList、Vector区别
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站