来源
计算概论05
描述
有一个分数序列 2/1,3/2,5/3,8/5,13/8,21/13,… 求这个分数序列的前n项之和。
关于输入
输入有一行:正整数n。
关于输出
输出有一行:分数序列的和(浮点数,精确到小数点后4位)。
可以printf("%.4lf\n", a)输出浮点数并精确到小数点后4位。
例子输入
99
例子输出
160.4849
#include <stdio.h>
int main() {
int n = 0;//pos_int n;
scanf("%d", &n);
double upper_num[1000] = { 0 };
double lower_num[1000] = { 0 };//the average of num is not fixed
int i;
upper_num[0] = 2.0;
upper_num[1] = 3.0;
lower_num[0] = 1.0;
lower_num[1] = 2.0;//first 4 fixed numbers(irregular
double summary = 3.5;
if (n == 1)printf("%.4lf\n", 2.0 / 1.0);
else if (n == 2)printf("%.4lf\n", 2.0 / 1.0 + 3.0 / 2.0);
else {
for (i = 2; i < n; i++) {
upper_num[i] = upper_num[i - 1] + upper_num[i - 2];
lower_num[i] = lower_num[i - 1] + lower_num[i - 2];
summary += upper_num[i] / lower_num[i];
}
printf("%.4lf\n", summary);
}
return 0;
}