首先是要导入的类包:
import java.util.*;
import java.math.*;
然后是具体框架:
public class Main
{
public static void main(String args[])
{
}
}
具体以 UVA 10254 The Priest Mathematician 为例:
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner ( System.in );
BigInteger dp[] = new BigInteger[10001];
BigInteger two = new BigInteger("2"); //大整数赋值
dp[0] = BigInteger.ZERO;
dp[1] = BigInteger.ONE;
int k = 1;
for (int i = 2; i <= 10000; ++i)
{
BigInteger add = BigInteger.ONE.shiftLeft(k);
for (int j = 0; j < k + 1 && i + j <= 10000; ++j)
{
dp[i + j] = dp[i + j - 1].add(add);
}
i += k;
++k;
}
while (cin.hasNextBigInteger())
{
int n = cin.nextInt();
System.out.println(dp[n]);
}
cin.close();
}
}
然后是大整数类中的一些函数:
BigInteger bi1 = new BigInteger("1234567890123456890");
BigInteger bi2 = BigInteger.valueOf(123L);
bi1=bi1.add(bi2);//b1+b2
bi1=bi1.multiply(bi2);//b1*b2
bi1=bi1.subtract(bi2);//b1-b2
bi1=bi1.divide(bi2);// b1/b2
if (bi1.compareTo(bi2) > 0) //如果bi1 > bi2。其它类似
//大浮点数
BigDecimal bd = new BigDecimal("3.1415926");
bd = bd.setScale(2,BigDecimal.ROUND_DOWN);//取3.1415926小数点后面二位
bd = bd.remainder(bi2); //取模
再举一个例子,用于EOF输入的:POJ 3181 Dollar Dayz
//* @author:alpc12
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
final int N = 1010;
BigInteger[] dp = new BigInteger[N];
void run() {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
int i, j;
int n = cin.nextInt(), k = cin.nextInt();
for(i = 1; i <= n; ++i)
dp[i] = BigInteger.ZERO;
dp[0] = BigInteger.ONE;
for(i = 1; i <= k; ++i) {
for(j = 0; j + i <= n; ++j)
dp[j+i] = dp[j+i].add(dp[j]);
}
System.out.println(dp[n]);
}
}
public static void main(String[] args) {
new Main().run();
}
}