2007年4月18日 星期三

Sorting

一、Write a program that can read an array of integers and sort the array of integers into ascending order or descending order. Your program must be able to
(1) Read the elements of the array from the keyboard.
(2) Use command-line arguments to pass either –a for ascending order or –d for descending order.
(3) Print the values of the array on the screen after ascending or descending.
(4) Reallocate the memory for the array to 1/N of the current number of elements、where N is any positive integer. Use command-line argument to pass N from the keyboard. Print the values remaining in the array.

<Examples of Input and Output>
Input: 5 4 8 3 20 11 –a Output: 3 4 5 8 11 20
Input: N = 2 Output: 3 4 5
Input: N = 3 Output: 3 4

Input: 5 4 8 3 20 11 –d Output: 20 11 8 5 4 3
Input: N = 2 Output: 20 11 8
Input: N = 3 Output: 20 11

困難度:*
時間複雜度:O(n^2)
程式語言:C++
預估時間:1 小時

解題原理:
1. 以字串讀入內容,一次處理一行
2. 判斷是遞增或遞減排序
3. 取得輸入的整數內容進行排序
4. 完成

程式下載


原碼下載


參考來源:中山大學八十九學年度電腦程式設計競賽 試題 一

沒有留言: