大一
题目:求斐波那契数列的前40项的和
问题:不知道为什么这样不行
#include<stdio.h>
int main()
{
long a=1,b=1,n=1,c;
while(n<38)
{
b+=a;
c=a;
n=n+1;
a=b;
b=c;
}
printf("%d\n",b);
} 本帖最后由 xieglt 于 2020-10-18 17:01 编辑
#include <stdio.h>
int main()
{
long a=1,b=1,n=1;
//定义一个求和变量
long sum = 0;
//f(x) = f(x-1) + f(x-2)
while(n<=20)
{
printf("%d,%d\n",a,b);
//sum + 数列第一项
sum += a;
//sum + 数列第二项
sum += b;
//后一项数 = 前两项数之和
a += b;
//后二项
b += a;
n=n+1;
}
printf("%d\n",sum);
return 0;
} fibo函数用递归写最简单
页:
[1]