当前位置:编程学习 > C#/ASP.NET >>

用C#求出数列2/1,3/2,5/3,8/5,13/8,21/13...的前20项之和

如题
谢谢! --------------------编程问答--------------------

        static void Main(string[] args)
        {
            int length = 20;
            double a, b, c;//分子
            int A, B, C;//分母

            a = 2; b = 3;
            A = 1; B = 2;

            double sum = a / A + b / B;
            c = a + b;
            C = A + B;
            for (int i = 2; i < length; i++)
            {
                Console.WriteLine(c + "/" + C);
                sum += c / C;

                a = b;
                b = c;
                c = a + b;

                A = B;
                B = C;
                C = A + B;
            }
            Console.WriteLine("=" + sum);

            Console.Read();
        }
--------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            double Total = 0;

            for (int i = 0; i < 20; i++)
            {
                double NR = GetN(2 + i);
                double MR = GetN(1 + i);
                double R = NR / MR;

                Total += R;
                Console.WriteLine(NR.ToString() + "/" + MR.ToString()
                    + "=" + R.ToString());
            }

            Console.WriteLine(Total.ToString());
            Console.Read();
        }

        static double GetN(int N)
        {
            if (N == 0 || N == 1)
                return 1;

            return GetN(N - 2) + GetN(N - 1);
        }
    }
}



0分?
作业吧 --------------------编程问答-------------------- 作业题发到这来还不给分,寒…… --------------------编程问答-------------------- 鉴定结果:超级懒人 --------------------编程问答--------------------
引用 4 楼 wujiaohua 的回复:
鉴定结果:超级懒人

鉴定有误,这不是懒,是根本就不会,只想粘贴 --------------------编程问答--------------------
引用 5 楼 llwinnner 的回复:
引用 4 楼 wujiaohua 的回复:
鉴定结果:超级懒人

鉴定有误,这不是懒,是根本就不会,只想粘贴

批准不准毕业 --------------------编程问答-------------------- for循环二十个数列的值。累加 --------------------编程问答-------------------- 自己再懒一个一个输入 然后相加也可以啊
超级懒 --------------------编程问答--------------------
引用 6 楼 hhc123 的回复:
引用 5 楼 llwinnner 的回复:
 引用 4 楼 wujiaohua 的回复:
 鉴定结果:超级懒人

 鉴定有误,这不是懒,是根本就不会,只想粘贴

 批准不准毕业

先抄代码一百遍 --------------------编程问答--------------------  public class Gradientcs3
    {
       public static double sum=0;
       static void Main()
       {
           int a = 1;
           int b = 1;           
           Sumarry(a, b,0);
       }
        public static void Sumarry(int a,int b,int count)
       {
           if (count == 20)
               return;
           sum +=(double)(a + b) / a;
           count++;
           Sumarry(a + b, a,count);
       }
    } --------------------编程问答-------------------- haha
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,