返回列表 發帖

C# 7 204 質數

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

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS02資料夾中的CSD02.cs進行編寫。依下列題意進行作答:輸入兩個數字,判斷及輸出二者的所有質數,使輸出值符合題意要求。檔案名稱請另存新檔為CSA02.cs,儲存於C:\ANS.CSF\CS02資料夾,再進行評分。

2. 設計說明:
請撰寫程式,讓使用者輸入兩個介於1至100之間的整數,輸出兩數間的所有質數。若輸入文字、帶有小數點的數字資料、小於等於0或大於100以外的數字,請輸出【error】。
* 提示:1不為質數。

3. 輸入輸出:
輸入說明
兩個介於1至100之間的整數

輸出說明
兩數間的所有質數

範例輸入1
79
100
範例輸出1
79
83
89
97

範例輸入2
0
25
範例輸出2
error

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

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

  6. namespace CSA02
  7. {
  8.     class CSA02
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             try
  13.             {
  14.                 int x1, x2;
  15.                 x1 = Convert.ToInt32(Console.ReadLine());
  16.                 x2 = Convert.ToInt32(Console.ReadLine());

  17.                 // swap(x1, x2) if x1 > x2
  18.                 int tmp;
  19.                 if (x1 > x2)
  20.                 {
  21.                     tmp = x1;
  22.                     x1 = x2;
  23.                     x2 = tmp;
  24.                 }

  25.                 // check the boundary conditions
  26.                 if (x1 <= 0 || x2 > 100)
  27.                 {
  28.                     throw new ArgumentException();
  29.                 }

  30.                 // 1 is not a prime
  31.                 bool foundFactor;
  32.                 for (int n = Math.Max(x1, 2); n <= x2; n++)
  33.                 {
  34.                     foundFactor = false;
  35.                     for (int k = 2; k < n; k++)
  36.                     {
  37.                         if (n % k == 0)
  38.                         {
  39.                             foundFactor = true;
  40.                             break; // it is not a prime
  41.                         }
  42.                     }
  43.                     if (!foundFactor)
  44.                     {
  45.                         Console.WriteLine(n);
  46.                     }
  47.                 }
  48.             }
  49.             catch
  50.             {
  51.                 Console.Write("error");
  52.             }
  53.             Console.ReadKey();
  54.         }


  55.     }
  56. }
複製代碼
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.     static void Main()//method ..Entry Point 程式進入點
  9.     {
  10.         try
  11.         {
  12.             int x1, x2;
  13.             x1 = Convert.ToInt32(Console.ReadLine());
  14.             x2 = Convert.ToInt32(Console.ReadLine());
  15.             int tmp;
  16.             if (x1 > x2)
  17.             {
  18.                 tmp = x1;
  19.                 x1 = x2;
  20.                 x2 = tmp;
  21.             }
  22.             if (x1 <= 0 || x2 > 100)
  23.                 throw new ArgumentException();//使用指定的錯誤訊息。
  24.             bool foundFactor;
  25.             for(int n=Math.Max(x1,2); n<=x2;n++)
  26.             {
  27.                 foundFactor = false;
  28.                 for(int k=2;k<n;k++)
  29.                 {
  30.                     if(n%k==0)
  31.                     {
  32.                         foundFactor = true;
  33.                         break;
  34.                     }
  35.                 }
  36.                 if(!foundFactor)
  37.                 {
  38.                     Console.WriteLine(n);
  39.                 }
  40.             }

  41.         }catch(Exception e)
  42.         {
  43.             Console.WriteLine(e.Message);
  44.             Console.WriteLine("error");

  45.         }
  46.     }
  47.     /*
  48.      * ArgumentException是一個自訂「合法參數」的例外,
  49.      * ArgumentException類別所接受的合法參數,是由程式設計師自行設定的。
  50.      *
  51.      * FormatException當參數格式不符合呼叫的方法的參數規範時引發的異常.
  52.      */


  53. }
複製代碼
istak.teach2@gmail.com

TOP

返回列表