JAVA大数高精度练习

3 篇文章 0 订阅

nyoj28 大数阶乘 http://acm.nyist.net/JudgeOnline/problem.php?pid=28

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.math.BigInteger;  
import java.util.*;  
import java.io.*;  
//求阶乘
public class Main{
     public static void main(String[] agrs){
         Scanner cin=new Scanner(System.in);
         int test=cin.nextInt();
         BigInteger ans=BigInteger.ONE;
         for(int i=1;i<=test;i++){
        	 ans=ans.multiply(BigInteger.valueOf(i));
         }
         System.out.println(ans);
    }
}</span>

nyoj45 棋盘覆盖 http://acm.nyist.net/JudgeOnline/problem.php?pid=45

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.math.BigInteger;  
import java.util.*;  
import java.io.*;  

public class Main {
	public static void main(String[] agrs){
		Scanner cin=new Scanner(System.in);
		int tc=cin.nextInt();
		while(tc-->0){
			int n;
			n=cin.nextInt();
			BigInteger ans=BigInteger.ONE;
			for(int i=1;i<=n;i++)
				ans=ans.multiply(BigInteger.valueOf(4));
			System.out.println(ans.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));
		}
	}
}
</span>

nyoj73 比较大小 http://acm.nyist.net/JudgeOnline/problem.php?pid=73

<span style="font-family:KaiTi_GB2312;font-size:18px;">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.valueOf(0))&&b.equals(BigInteger.valueOf(0)))
				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");
			
		}
	}
}
</span>


nyoj103 A+B Problem2http://acm.nyist.net/JudgeOnline/problem.php?pid=103

<span style="font-family:KaiTi_GB2312;font-size:18px;">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);
		int tc=cin.nextInt();
		for(int i=1;i<=tc;i++){
			BigInteger a,b;
			a=cin.nextBigInteger();
			b=cin.nextBigInteger();
			BigInteger c=a.add(b);
//			if(i>1)
//				System.out.println();//加上这两行过hdu 1002
			System.out.println("Case "+i+":");
			System.out.println(a+" + "+b+" = "+c);
		}
	}
}
</span>

nyoj 114某种序列 http://acm.nyist.net/JudgeOnline/problem.php?pid=114

<span style="font-family:KaiTi_GB2312;font-size:18px;">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);
		BigInteger a[]=new BigInteger[100];
		while(cin.hasNext()){
			for(int i=0;i<3;i++)
				a[i]=cin.nextBigInteger();
			for(int i=3;i<100;i++)
				a[i]=a[i-3].add(a[i-2].add(a[i-1]));
			System.out.println(a[99]);
		}
	}
}
</span>

nyoj155 求高精度幂 http://acm.nyist.net/JudgeOnline/problem.php?pid=155

<span style="font-family:KaiTi_GB2312;font-size:18px;">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();
//			for(int i=1;i<=n;i++)
//				ans=ans.multiply(ans);
			String res=ans.pow(n).stripTrailingZeros().toPlainString();//整数去掉后面的小数点和9=0
			if(res.startsWith("0")){
				res=res.substring(1);
			}
			System.out.println(res);
		}
		
		
	}
}
</span>




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值