标签:lin next false add 代码 names else cst head
#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;
}
A1052 Linked List Sorting (25分)
标签:lin next false add 代码 names else cst head
原文地址:https://www.cnblogs.com/tsruixi/p/12256623.html