1. 程式人生 > >牛客網 - 線上程式設計 - 華為機試 - 字串字典順序排序

牛客網 - 線上程式設計 - 華為機試 - 字串字典順序排序

題目描述
給定n個字串,請對n個字串按照字典序排列。
輸入描述:

輸入第一行為一個正整數n(1≤n≤1000),下面n行為n個字串(字串長度≤100),字串中只含有大小寫字母。

輸出描述:

資料輸出n行,輸出結果為按照字典序排列的字串。

示例1
輸入

9
cap
to
cat
card
two
too
up
boat
boot

輸出

boat
boot
cap
card
cat
to
too
two
up

C++:

#include<iostream>
#include<string> #include<vector> #include<algorithm> using namespace std; int main() { int n; while (cin >> n) { cin.get(); //吸收輸入數字後的回車符 vector<string> a{}; while (n--) { string s; getline(cin, s); a.push_back(s); } sort(a.begin(), a.end()); for
(int i = 0; i < a.size(); i++) { cout << a[i] << endl; } } cin.get(); return 0; }

Python:

while 1:
    try:
        n = int(input())
        a = []
        while(n):
            a.append(input())
            n -= 1
        a = sorted(a)
        for
i in a: print(i) except: break