美文网首页工作生活
PAT 1075 链表元素分类 (25 分)

PAT 1075 链表元素分类 (25 分)

作者: 昭明ZMing | 来源:发表于2019-07-03 10:42 被阅读0次
    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 100000;
    int first, N, K;
    struct Node{
        int address;
        int date;
        int next;
    };
    Node n;
    vector<Node> l;
    vector<Node> ans;
    Node add[maxn];
    int main() {
        scanf("%d%d%d", &first, &N, &K);
        for(int i = 0; i < N; i ++) {
            scanf("%d%d%d", &n.address, &n.date, &n.next);
            add[n.address] = n;
        }
        while(first != -1) {
            l.push_back(add[first]);
            first = add[first].next;
        }
        int sz = l.size();
        for(int i = 0; i < sz; i ++) {
            if(l[i].date < 0)
                ans.push_back(l[i]);
        }
        for(int i = 0; i < sz; i ++) {
            if(l[i].date >= 0 && l[i].date <= K)
                ans.push_back(l[i]);
        }
        for(int i = 0; i < sz; i ++) {
            if(l[i].date > K)
                ans.push_back(l[i]);
        }
        for(int i = 0; i < sz; i ++) {
            ans[i].next = ans[i + 1].address;
            if(i != sz - 1)
                printf("%05d %d %05d\n", ans[i].address, ans[i].date, ans[i].next);
            else
                printf("%05d %d -1\n", ans[i].address, ans[i].date);
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PAT 1075 链表元素分类 (25 分)

        本文链接:https://www.haomeiwen.com/subject/lizfhctx.html