C语言实验——分数序列

来源:互联网 发布:淘宝买家秀模板 编辑:程序博客网 时间:2024/05/16 17:33

C语言实验——分数序列

Time Limit: 1000MS Memory Limit: 65536KB

Submit Statistic

Problem Description

有一个分数序列:2/1, 3/2, 5/3, 8/5, 13/8, …编写程序求出这个序列的前n项之和。

Input

输入只有一个正整数n,1≤n≤10。

Output

输出该序列前n项和,结果保留小数后6位。

Example Input

3

Example Output

5.166667

Hint

Author

#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    int n,i;    double a1,b1,b2,a3,b3,a2,s;//a1,a2,a3,b1,b3,b2分别存放分子和分母;    scanf("%d",&n);    for(i = 1;i <= n;i++)//分析题意可知,第三个分母与分子分别是前两个分母与分子的和;    {        if(i==1)        {            a1 = 2;            b1 = 1;            s = s + a1*1.0/b1;        }        else if(i==2)        {            a2 = 3;            b2 = 2;            s = s + a2*1.0/b2;        }        else        {            a3 = a1 + a2;            b3 = b1 + b2;            s = s + a3*1.0/b3;            a1 = a2;            a2 =  a3;            b1 = b2;            b2 = b3;        }    }    printf("%.6lf\n",s);    return 0;}
原创粉丝点击