A1052 Linked List Sorting (25分)

一、技术总结

  1. 这个也是一个链表类题目,主要是在结构提上的设计,可以设计一个flag参数用于记录真实有效的结点。
  2. 然后就是根据题目要求进行排序,输出结果。
  3. cmp()函数可以,一层是把有效节点排到数组的左边,然后可以进行二次比较,按题目要求来。

二、参考代码

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct Node{
    int key;//数据
    int next;//用于存储下一个地址
    int address;//地址
    bool flag;
}node[100010];
bool cmp(Node a, Node b){
     if(a.flag == false || b.flag == false){
        return a.flag > b.flag;
     }else{
        return a.key < b.key;
     }
}
int main(){
    int N, head;
    int cnt = 0;//用于统计有效节点个数
    scanf("%d %d", &N, &head);
    for(int i = 0; i < N; i++){
        int tkey, tnext, taddress;
        scanf("%d %d %d", &taddress, &tkey, &tnext);
        node[taddress] = {tkey, tnext, taddress, false};
    }
    for(int i = head; i != -1; i = node[i].next){
        cnt++;
        node[i].flag = true;
    }
    if(cnt == 0){
        printf("0 -1");
    }else{
        sort(node, node+100010, cmp);
        printf("%d %05d\n", cnt, node[0].address);
        for(int i = 0; i < cnt; i++){
            if(i == cnt-1){
                printf("%05d %d -1", node[i].address, node[i].key);
            }else{
                printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+1].address);
            }
        }
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/tsruixi/p/12256623.html

时间: 2024-10-08 08:01:53

A1052 Linked List Sorting (25分)的相关文章

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

A1052. Linked List Sorting (25)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the stru

1052 Linked List Sorting (25分)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the stru

链表_A1052 Linked List Sorting (25 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 /* *链表的处理 *1.定义静态链表,结构体数组 *2.初始化falg为false *3.从链表首地址begin遍历,并标记有效结点 *4.对结点排序,有效结点true大于false */ #include<iostream> using namespace std; #include<cstdio> #include<

【PAT甲级】1052 Linked List Sorting (25 分)

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

pat1052. Linked List Sorting (25)

1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key a

PAT 1052. Linked List Sorting (25)

1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

02-线性结构3 Reversing Linked List (25 分)

02-线性结构3 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must ou