您的位置:首页 > 编程语言 > Java开发

JAVA BigInteger(大数类)HDU 1002 1042

2016-03-10 16:28 591 查看
总结一下用到的大数类的基本方法:

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 stub
Scanner 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 stub
int 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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java HDU 大数 BigInteger