日期:2014-05-20 浏览次数:20616 次
import java.math.BigDecimal; public class Test { public static BigDecimal calcSum(BigDecimal[] arr, int num) { if (num <= 0 || num > arr.length) { return null; } BigDecimal big = new BigDecimal(0); for (int i = 0; i < num; i++) { big = big.add(arr[i]); } return big; } public static void main(String[] args) { MyArr myArr = new MyArr(1000); System.out.println(calcSum(myArr.getArr(), 1000)); } } class MyArr { private BigDecimal[] arr; public BigDecimal[] getArr() { return this.arr; } public MyArr(int length) { if (length < 2 || length > 1000) { return; } arr = new BigDecimal[length]; arr[0] = new BigDecimal(1); arr[1] = new BigDecimal(2); for (int i = 2; i < length; i++) { arr[i] = arr[i-1].add(arr[i-2]); } } }