求2/1, 3/2, 5/3, 8/5, 13/8, 21/13,…的前二十项和,我的答案一直错,麻烦帮我看一下我的代码有什么问题。
#include<stdio.h>int main(){
int a,b,i;
a=1;b=2;
float sum=0,c;
for(i=1;i<=20;i++){
a=b;
b=a+b;
c=b/a;
sum=sum+c;
}
printf("%f",sum);
return 0;
}
[此贴子已经被作者于2021-11-28 21:48编辑过]
#include<stdio.h> void main() { float s = 0, tmp; int i; for (i=0,tmp=2; i<20; i++)//由题目可知,首项值为2,其他项的关系为 f(n+1) = 1 + 1/f(n) { s += tmp; tmp = 1 + 1/tmp; } printf("%f", s); }
#include <stdio.h> int main() { int a, b, i; a = 1; b = 2; double sum = 0; for (i = 1; i <= 20; i++) { sum += 1.0 * b / a; b = a + b; a = b - a; } printf("%f", sum); return 0; }