伊莉討論區

標題: [問題]三個數排大小求幫找錯誤 [打印本頁]

作者: pftest1033214    時間: 2017-10-5 09:47 PM     標題: [問題]三個數排大小求幫找錯誤

[attach]120522594[/attach]
作者: rr09192084    時間: 2017-10-6 04:44 PM

本帖最後由 rr09192084 於 2017-10-7 09:15 AM 編輯
  1. static void Main(string[] args)
  2.         {
  3.             double a, b, c, max, second, third;
  4.             string inputa;
  5.             do
  6.             {
  7.                 Console.Write("Type a =");
  8.                 inputa = Console.ReadLine();
  9.             } while (!Double.TryParse(inputa, out a));

  10.             string inputb;
  11.             do
  12.             {
  13.                 Console.Write("Type b =");
  14.                 inputb = Console.ReadLine();
  15.             } while (!Double.TryParse(inputb, out b));

  16.             string inputc;
  17.             do
  18.             {
  19.                 Console.Write("Type c =");
  20.                 inputc = Console.ReadLine();
  21.             } while (!Double.TryParse(inputc, out c));

  22.             double[] ary = { a, b, c };
  23.             max = Max(ary);
  24.             second = Second(ary);
  25.             third = Third(ary);
  26.             Console.Write("Max = " + max + " ");
  27.             Console.Write("Second = "+second + " ");
  28.             Console.Write("Third =" + third);
  29.             Console.ReadKey();
  30.         }
  31.         private static double Max(double[] ay)
  32.         {
  33.             Double MaxN = ay[0];
  34.             foreach (Double m  in ay)
  35.             {
  36.             if (m > MaxN)
  37.             MaxN = m;
  38.             }
  39.             return MaxN;
  40.         }
  41.         private static double Second(double[] ay)
  42.         {
  43.             double mx = Max(ay);
  44.             double seN = ay[0];
  45.             foreach (Double m in ay)
  46.             {
  47.                 if (m < mx)
  48.                 {
  49.                     if (m > seN)
  50.                         seN = m;
  51.                 }
  52.             }
  53.             return seN;
  54.         }
  55.       
複製代碼
你這是C語言的,我寫了一個C#的你參考看看。
作者: rr09192084    時間: 2017-10-7 09:18 AM

  1.         private static double Third(double[] ay)
  2.         {
  3.             double thirdN = ay[0];
  4.             double seN = Second(ay);
  5.             foreach (Double m in ay)
  6.             {
  7.                 if (m < seN)
  8.                 {
  9.                     if (m >= thirdN)
  10.                         thirdN = m;
  11.                 }
  12.             }
  13.             return thirdN;
  14.         }
複製代碼
補充求第三大數字的函式

作者: rr09192084    時間: 2017-10-8 08:49 AM

  1.         static void Main(string[] args)
  2.         {
  3.             double a, b, c, max, second, third;
  4.             string inputa;
  5.             do
  6.             {
  7.                 Console.Write("Type a =");
  8.                 inputa = Console.ReadLine();
  9.             } while (!Double.TryParse(inputa, out a));

  10.             string inputb;
  11.             do
  12.             {
  13.                 Console.Write("Type b =");
  14.                 inputb = Console.ReadLine();
  15.             } while (!Double.TryParse(inputb, out b));

  16.             string inputc;
  17.             do
  18.             {
  19.                 Console.Write("Type c =");
  20.                 inputc = Console.ReadLine();
  21.             } while (!Double.TryParse(inputc, out c));

  22.             double[] ary = { a, b, c };
  23.             bubbleSort(ary);
  24.             max = ary[0];
  25.             second = ary[1];
  26.             third = ary[2];
  27.             Console.Write("Max = " + max + " ");
  28.             Console.Write("Second = "+second + " ");
  29.             Console.Write("Third =" + third + "\r\nPress any key to quit....");
  30.             Console.ReadKey();
  31.         }

  32.         public static void bubbleSort(double[] list)
  33.         {
  34.             int n = list.Length;
  35.             double temp;
  36.             int Flag = 1; //旗標
  37.             int i;
  38.             for (i = 1; i <= n - 1 && Flag == 1; i++)
  39.             {    // 外層迴圈控制比較回數
  40.                 Flag = 0;
  41.                 for (int j = 1; j <= n - i; j++)
  42.                 {  // 內層迴圈控制每回比較次數            
  43.                     if (list[j] > list[j - 1])
  44.                     {  // 比較鄰近兩個物件,右邊比左邊大時就互換,降冪排序,此地改小於就是升冪排序。               
  45.                         temp = list[j];
  46.                         list[j] = list[j - 1];
  47.                         list[j - 1] = temp;
  48.                         Flag = 1;
  49.                     }
  50.                 }
  51.             }
  52.         }
複製代碼
我重複試了幾次,發現上之前的方法有問題,所以改了用降冪排序法,這樣就保證前3個元素就是正解了。
作者: pftest1033214    時間: 2017-10-8 10:18 AM

謝謝大大 終於做出來了  中秋連假祝大大玩得愉快




歡迎光臨 伊莉討論區 (http://www02.eyny.com/) Powered by Discuz!