public class Fibonacci {
//非递归求解第n项
public int fib(int n){
if(n == 1 || n == 2){
return 1;
}
int low = 1;
int high = 1;
int sum = 0;
for(int i = 3;i <= n;i++){
sum = low + high;
low = high;
high = sum;
}
return sum;
}
public static void main(String[] args) {
Fibonacci fib = new Fibonacci();
System.out.println(fib.fib(30));
}
}
非递归求解斐波那契数列第n项的值
最新推荐文章于 2021-04-09 22:33:57 发布