题解 | 【模板】序列操作

【模板】序列操作

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")

全部评论

相关推荐

点赞 评论 收藏
分享
开发转测第二人:没实习的话,两个项目吧,八股也要准备一下,这个时间点有点小晚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务