25、有一分数序列:2/1,3/2,5/3.8/5,13/8,'''求前20项之和
作者:互联网
/*
有一分数序列:2/1,3/2,5/3.8/5,13/8,'''求前20项之和
*/
#include <stdio.h>
#include <stdlib.h>
double getSum(int n)
{
double fenzi = 2;
double fenmu = 1;
// int fenzi = 2;
// int fenmu = 1;
double sum = 0;
for(int cnt = 1;cnt <= n;cnt ++)
{
// sum += fenzi * 1.0 / fenmu;
sum += (double) fenzi / fenmu;
fenzi += fenmu;
fenmu = fenzi - fenmu;
// fenmu = fenzi ; 错误写法!!!!!
// fenzi += fenmu ;
}
return sum;
}
int main()
{
int n;
scanf("%d", &n);
printf("%f\n", getSum(n));
int a = 3;
int b = 2;
double c = 1;
c += (double) a / b;
printf("%f", c);
return 0;
}
标签:25,13,20,int,求前,cnt,double 来源: https://www.cnblogs.com/buwenliunian/p/15335296.html