总结一下用到的大数类的基本方法:
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的BigInteger类进行大数运算的基本方法,包括加法、乘法、除法、取余、取相反数及求幂等,并通过两个实战案例展示了如何利用这些方法解决实际问题。
HDU 1002 1042&spm=1001.2101.3001.5002&articleId=50847677&d=1&t=3&u=4c2d54ce1c0b4e77a736821360a67d2b)
3787

被折叠的 条评论
为什么被折叠?



