C#小練習(設計一個程式,輸入10個數存入陣列中,然後實現氣泡排序。 )
阿新 • • 發佈:2019-01-25
/* (程式頭部註釋開始) * 程式的版權和版本宣告部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 檔名稱: * 作 者: 臧鵬 * 完成日期: 2012 年 9月 16日 * 版 本 號: 001 * 對任務及求解方法的描述部分 * 輸入描述: * 問題描述:設計一個程式,輸入10個數存入陣列中,然後實現氣泡排序。 * 程式輸出: * 程式頭部的註釋結束 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { int[] num = new int[10]; Console.WriteLine("請輸入十個數:"); for (int i = 0; i < 10; i++) { string s = Console.ReadLine(); int x = int.Parse(s); num[i] = x; } int j ; int n ; for (j = 0; j < 10; j++) { for (n = 0; n < 9 - j; n++) { if (num[n] > num[n + 1]) { int m; m = num[n]; num[n] = num[n + 1]; num[n + 1] = m; } } } for(n =0;n<10;n++) { Console .WriteLine (num[n]); } Console.ReadKey(false); } } }