c语言练习24——数列求和

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 //题目:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
 4 int main(){
 5 int a,b,i,n,t;
 6 float s;
 7 printf("请输入相加项数:");
 8 scanf("%d",&n);
 9 s=0;
10 a=2;
11 b=1;
12 for(i=1;i<=n;i++){
13     s=s+a/b;
14     t=a;
15     a=a+b;
16     b=t;
17 }
18 printf("%f",s);
19 return 0;
20 }
21    

猜你喜欢

转载自www.cnblogs.com/gougouwang/p/11410403.html