返回列表 發帖

C# 7 305 費氏數列

TQC+ 物件導向程式語言
最新一次更新時間:2024-01-05 14:50:31

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS03資料夾中的CSD03.cs進行編寫。依下列題意進行作答:建立費氏數列,再輸出指定位置的數值,使輸出值符合題意要求。檔案名稱請另存新檔為CSA03.cs,儲存於C:\ANS.CSF\CS03資料夾,再進行評分。

2. 設計說明:
費氏數列為0、1、1、2、3、5、8、13、21、34、55、…,第一個數為0,第二個數為1,其它的數為前面兩個數的和。
請撰寫一程式,建立含有前50個費氏數列的陣列,讓使用者輸入一個整數n,做為陣列的索引值,並輸出陣列中的第n+1個費氏數列,如輸入n=4,輸出【5:3】,表示輸出第5個費式數列值為3,中間以一個半形冒號分隔,若輸入文字或不存在的索引值,請輸出【error】。
*提示:使用long型態。

3. 輸入輸出:
輸入說明
一個整數n

輸出說明
陣列中的第n+1個費氏數列(輸出最後一行後不自動換行)

範例輸入1
4
範例輸出1
5:3

範例輸入2
pi
範例輸出2
error

4. 評分項目:
(1) 符合設計說明輸出正確格式配分        20
May

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace CSA03
  7. {
  8.     class CSA03
  9.     {
  10.       
  11.         static void Main(string[] args)
  12.         {
  13.             try
  14.             {
  15.                 long[] fs = new long[50];
  16.                 fs[0] = 0;
  17.                 fs[1] = 1;
  18.                 for(int i = 2;i <= fs.Length-1; i++)
  19.                 {
  20.                     fs[i] = fs[i - 1] + fs[i - 2];
  21.                 }
  22.                 int index = Convert.ToInt32(Console.ReadLine());
  23.                 Console.Write("{0}:{1}", (index + 1).ToString(),fs[index].ToString());
  24.             }
  25.             catch
  26.             {
  27.                Console.Write("error");
  28.             }
  29.             Console.ReadKey();
  30.         }



  31.     }
  32. }
複製代碼
May

TOP

  1. using ConsoleApp1;
  2. using System;//程式庫呼叫
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq.Expressions;
  5. using ABC.qq;


  6. class Program//負責一部分工作的人
  7. {
  8.    
  9.     static void Main()
  10.     {
  11.         try
  12.         {
  13.             long[] fs = new long[50];
  14.             fs[0] = 0;
  15.             fs[1] = 1;
  16.             for(int i=2;i<fs.Length;i++)
  17.             {
  18.                 fs[i]= fs[i - 1] + fs[i - 2];
  19.             }
  20.             int index=Convert.ToInt32(Console.ReadLine());
  21.             Console.WriteLine("{0}:{1}", (index + 1), fs[index]);

  22.         }catch
  23.         {
  24.             Console.WriteLine("error");
  25.         }
  26.         Console.ReadKey();
  27.     }
  28.    
  29. }
複製代碼
istak.teach2@gmail.com

TOP

返回列表