public class Fabnacci {
private static List fabList = new ArrayList();
static {
fabList.add(1L);
fabList.add(1L);
}
public static List generate(int length) {
int size = fabList.size();
if (size < length) {
for (int i = 0; i < length - size; i++) {
fabList.add(fabList.get(size + i - 2) + fabList.get(size + i - 1));
}
}
return fabList.subList(0, length);
}
public static void main(String[] args) {
System.out.println(Fabnacci.generate(10));
}
}
Fabnacci.generate(10),如果设置10就输出10个,设置20就输出20个;
希望对你有所帮助
取消
评论