題目敘述
請設計一個程式,此程式可以不斷地輸入一個N值,直到此N值小於等於0或大於20時離開程式。當一個大於0且小於等於20的N值輸入時,此程式可以讀取N個最大可到1000位數的整數,請將N個值讀入後由小到大排序並列印出來。
參考答案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
while(cin >> n and n > 0) {
vector<string> vec;
string str;
while(n--) {
cin >> str;
vec.push_back(str);
}
sort(vec.begin(), vec.end(), [](string a, string b) {
if(a.length() == b.length()) {
int i = 0;
for(; i < a.length() and a[i] == b[i]; i++);
return a[i] < b[i];
}
return a.length() < b.length();
});
for(string &x: vec)
cout << x << endl;
}
return 0;
}
|