1052. Linked List Sorting (25) 22'

简介: #include #include #include using namespace std;/* 题目大意:对结构体进行排序 分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表, */...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
 题目大意:对结构体进行排序
 分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表,
 */
struct node{
    int adress, data, next;
} nodes[100001];

bool cmp(node a, node b){
    return a.data < b.data;
}

int main(){
    int n, s;
    cin >> n >> s;
    vector<node> v;
    for (int i = 0; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        nodes[a] = {a, b, c};
    }
    int head = s;
    while (head != -1) {
        v.push_back(nodes[head]);
        head = nodes[head].next;
    }

    sort(v.begin(), v.end(), cmp);

    if (v.size() == 0) {
        cout << "0 -1\n";
    }else{
        cout << v.size() << ' ' << v[0].adress << endl;
        for (int i = 0; i < v.size(); i++) {
            if(i == 0) printf("%05d %d ", v[i].adress, v[i].data);
            else printf("%05d\n%05d %d ", v[i].adress, v[i].adress, v[i].data);
        }
        cout << "-1\n";
    }

    return 0;
}
目录
相关文章
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
75 0
|
存储 C++
【PAT甲级 - C++题解】1028 List Sorting
【PAT甲级 - C++题解】1028 List Sorting
70 0
【1028】List Sorting (25 分)
【1028】List Sorting (25 分) 【1028】List Sorting (25 分)
95 0
|
固态存储 SDN
1028 List Sorting (25)
#include #include #include #include #include using namespace std; int c; struct node{ string id, name;...
855 0
|
算法 人工智能 SDN
算法学习之路|List Sorting
Excel can sort records according to any column. Now you are supposed to imitate this function.
973 0
|
3月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
408 1
|
2月前
|
Java API Apache
怎么在在 Java 中对List进行分区
本文介绍了如何将列表拆分为给定大小的子列表。尽管标准Java集合API未直接支持此功能,但Guava和Apache Commons Collections提供了相关API。
|
2月前
|
运维 关系型数据库 Java
PolarDB产品使用问题之使用List或Range分区表时,Java代码是否需要进行改动
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
2月前
|
存储 安全 Java
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
|
3月前
|
Java API
使用 Java 来实现两个 List 的差集操作
使用 Java 来实现两个 List 的差集操作
54 3