1131 -- 数列求和

来源:互联网 发布:查找重复删除知乎 编辑:程序博客网 时间:2024/06/08 07:52

数列求和

Time Limit:2000MS  Memory Limit:65536K
Total Submit:294 Accepted:154

Description

有一个分数序列:2/1,3/2,5/3,8/5,13/8 。。。。
输入一数,求出这个数列前n项之和(n<20)。

Input

输入为一个整数,表示要求的前几项数列

Output

以浮点数输出前n项的和

Sample Input

样例输入:2

Sample Output

样例输出:3.500

Source

    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    namespace AK1131 {        class Program {            static void Main(string[] args) {                int n = int.Parse(Console.ReadLine());                int a = 2, b = 1;                double sum = 0;                for (int i = 0; i < n; i++) {                    sum += a * 1.0 / b;                    int c = a;                    a = a + b;                    b = c;                }                Console.WriteLine(sum.ToString("0.000"));                //Console.ReadLine();            }        }    }


0 0