主要是运用了库函数 java.math.BigInteger
把大数转换成字符串进行运算
package 大数操作;
import java.math.BigInteger;
public class 大数处理BigInteger {
public static int countWays(int n) {
BigInteger[] a = new BigInteger[n + 1];
if (n == 1)
return 0;
else if (n == 2)
return 1;
else if (n == 3)
return 2;
else {
a[1] = BigInteger.ZERO;
a[2] = BigInteger.ONE;
a[3] = a[2].add(a[2]);
for (int i = 4; i <= n; i++) {
a[i] = a[i - 1].add( a[i - 2]);
}
BigInteger b = new BigInteger("1000000007");
return a[n].mod(b).intValue();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Integer.MAX_VALUE+" "+countWays(99));
}
}