题解 | 【模板】序列操作
【模板】序列操作
https://www.nowcoder.com/practice/12da4185c0bb45918cfdc3072e544069
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
long getNthNum(string str, int n) {
stringstream ss(str);
string substring;
for (int i = 0; i < n; i++) {
ss >> substring;
}
return stol(substring);
}
int main() {
string opt;
vector<long> lst;
while (getline(cin, opt)) { // 注意 while 处理多个 case
int idx = getNthNum(opt, 1);
switch (idx) {
case 1:
lst.push_back(getNthNum(opt, 2));
break;
case 2:
if (!lst.empty())
lst.pop_back();
break;
case 3:
cout << lst[getNthNum(opt, 2)] << endl;
break;
case 4:
lst.insert(lst.begin() + getNthNum(opt, 2) + 1, getNthNum(opt, 3));
break;
case 5:
sort(lst.begin(), lst.end());
break;
case 6:
sort(lst.rbegin(), lst.rend());
break;
case 7:
cout << lst.size() << endl;
break;
case 8:
if (!lst.empty()) {
for (int i = 0; i < lst.size(); i++) {
cout << lst[i];
if (i < lst.size() - 1) {
cout << " ";
}
}
cout << endl;
}
break;
}
}
}
// 64 位输出请用 printf("%lld")
