因为你的参数给的是int n,所以这个n我就不用BigInteger了。static public BigInteger fibonacci(int n)
{
int i =0;
BigInteger a = new BigInteger("0");
BigInteger result = new BigInteger("1");
BigInteger tmp = null;
while(i<n)
{
tmp = result;
result = result.add(a);
a = tmp;
i++;
}
return result;
}
{
int i =0;
BigInteger a = new BigInteger("0");
BigInteger result = new BigInteger("1");
BigInteger tmp = null;
while(i<n)
{
tmp = result;
result = result.add(a);
a = tmp;
i++;
}
return result;
}
public static void main(String[] args) { for (int i = 0; i < 100; i++) {
System.out.println(fibonacci(i));
} }
但要是用递归写会不会好点呢,我试着写了一个,但抱错了!
大大能看看什么毛病吗?
static public BigInteger fibonacci(int n){
if (n==1) return new BigInteger("0");
if (n==2) return new BigInteger("1");
if(n>2) return fibonacci(n-1).add(fibonacci(n-2);
}