#include "stdio.h"#include "math.h"
main(){ int i,n; float up=2,down=1,sum=0;
printf("请输入n\nn="); scanf("%d",&n);
for(i=1;i<=n;i++) { sum+=up/down; up+=down; down=up-down; }
printf("result is:%f\n",sum);}
n可以由键盘输入
#include <stdio.h>#include <stdlib.h>
int main(void){ int n, i, f1 = 1, f2 = 2, f = 0, temp;
scanf("%d", &n); for(i = 0;i < n;i ++) { f += f2/f1; temp = f1; f1 = f2; f2 = temp + f2; }
printf("%d\n", f); return 0;}
int main(void){ int n, i; float f1 = 1, f2 = 2, s= 0;
scanf("%d", &n);
for(i =1;i <=n;i++) { s+= f2/f1; f2=f2+f1; f1=f2-f1; }
printf("%f \n", s); getch(); return 0;}
都是高手啊呵呵~
for(i=1;i<=n;i++) { sum+=up/down; up+=down; down=up-down; /*这里比较难想到*/ }