JAVA BigInteger(大数类)HDU 1002 1042

来源:互联网 发布:淘宝 营销热词 编辑:程序博客网 时间:2024/05/16 01:19

总结一下用到的大数类的基本方法:

1.大数加法:

add ( BigInteger  val )

2.大数乘法:

multiply ( BigInteger val )

3.大数除法:

divide ( BigInteger val )

4.大数取余:

mod ( BigInteger val )

5.取相反数:

negate ( )

6.求幂

pow( int number )

HDU 1002:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

代码如下:

import java.math.BigInteger;import java.util.*;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner cin=new Scanner(System.in);BigInteger a,b,sum;int T,index;T=cin.nextInt();index=0;while(T>0){T--;index++;a=cin.nextBigInteger();b=cin.nextBigInteger();sum=a.add(b);System.out.println("Case " + index + ":");System.out.println(a + " + " + b + " = " + sum);if(T!=0)System.out.println();}}}


HDU 1042

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

代码如下:

import java.math.BigInteger;import java.util.*;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubint i,n;BigInteger Sum,ad,temp;Scanner cin= new Scanner(System.in);while(cin.hasNext()){n=cin.nextInt();Sum=new BigInteger("1");ad=new BigInteger("1");temp=new BigInteger("1");for(i=2;i<=n;i++){ad=ad.add(temp);Sum=Sum.multiply(ad);}System.out.println(Sum);}}}


1 0
原创粉丝点击