测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A41045. 斐波纳契数列

填空题 中等

题目描述

斐波纳契数列

题目描述

斐波纳契数列

1,1,2,3,5,8,13,21,34,55,89……这个数列则称为“斐波纳契数列”,其中每个数字都是“斐波纳契数”。

输入格式

一个整数N(N不能大于40)

输出格式

由N个“斐波纳契数”组成的“斐波纳契数列”。

样例输入

6

样例输出

1 1 2 3 5 8

参考答案

#include <stdio.h> #include <string.h> long fibonacci(int n) { if (n <= 2) { return 1; } long result; long pre_result; long pre_pre_result; pre_result = pre_pre_result = 1; while (n > 2) { result = pre_result + pre_pre_result; pre_pre_result = pre_result; pre_result = result; n--; } return result; } int main() { int N; scanf("%d", &N); int i; for (i = 1; i <= N; i++) { printf("%ld ", fibonacci(i)); } return 0; }
上一题 下一题