- 最後登錄
- 2022-11-24
- 在線時間
- 1 小時
- 註冊時間
- 2008-9-2
- 閱讀權限
- 20
- 精華
- 0
- UID
- 4896884
- 帖子
- 541
- 積分
- 212 點
- 潛水值
- 13707 米
| 所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。 - static void Main(string[] args)
- {
- double a, b, c, max, second, third;
- string inputa;
- do
- {
- Console.Write("Type a =");
- inputa = Console.ReadLine();
- } while (!Double.TryParse(inputa, out a));
- string inputb;
- do
- {
- Console.Write("Type b =");
- inputb = Console.ReadLine();
- } while (!Double.TryParse(inputb, out b));
- string inputc;
- do
- {
- Console.Write("Type c =");
- inputc = Console.ReadLine();
- } while (!Double.TryParse(inputc, out c));
- double[] ary = { a, b, c };
- bubbleSort(ary);
- max = ary[0];
- second = ary[1];
- third = ary[2];
- Console.Write("Max = " + max + " ");
- Console.Write("Second = "+second + " ");
- Console.Write("Third =" + third + "\r\nPress any key to quit....");
- Console.ReadKey();
- }
- public static void bubbleSort(double[] list)
- {
- int n = list.Length;
- double temp;
- int Flag = 1; //旗標
- int i;
- for (i = 1; i <= n - 1 && Flag == 1; i++)
- { // 外層迴圈控制比較回數
- Flag = 0;
- for (int j = 1; j <= n - i; j++)
- { // 內層迴圈控制每回比較次數
- if (list[j] > list[j - 1])
- { // 比較鄰近兩個物件,右邊比左邊大時就互換,降冪排序,此地改小於就是升冪排序。
- temp = list[j];
- list[j] = list[j - 1];
- list[j - 1] = temp;
- Flag = 1;
- }
- }
- }
- }
複製代碼 我重複試了幾次,發現上之前的方法有問題,所以改了用降冪排序法,這樣就保證前3個元素就是正解了。... |
|