PAT 1074 Reversing Linked List [静态链表] [无效结点]

2024-04-09 11:32

本文主要是介绍PAT 1074 Reversing Linked List [静态链表] [无效结点],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10​5​​) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

---------------------------------------这是题目和解题的分割线---------------------------------------

和这道题的思路差不多吧PAT 1097 Deduplication on a Linked List,但是我脑子大概是进浆糊了,最后两个测试点卡了好久,最后发现一个是没看清题目,是反转链表,即按照id倒序输出而不是按照data倒序= = 还有一个是反转的次数是有效结点/K,而不是N/K(明明注意到这一点但傻了忘了这个地方也要改 = = )链表(静态)的题都要记得小心无效结点

大概思路是先按照单链表的顺序排序,再按照k分组排序。

#include<cstdio>
#include<algorithm>const int maxN = 100010;using namespace std;struct node
{int data;int address; //当前地址 int next;int id; //链表次序 
}list[maxN];bool cmp1(node a,node b)
{return a.id<b.id;
}bool cmp2(node a,node b)
{return a.id>b.id;
}int main()
{int begin,n,k,i,j;scanf("%d%d%d",&begin,&n,&k);int address;for(i=0;i<n;i++){scanf("%d",&address);scanf("%d%d",&list[address].data,&list[address].next);list[address].address = address;}//初始化成超大的数,以便排序 for(i=0;i<maxN;i++)list[i].id = 2*maxN;int p = begin,order = 0;//遍历,并标上序号 while(p!=-1){list[p].id = order++;p = list[p].next;}//按单链表的顺序排序 sort(list,list+maxN,cmp1);//是order/k不是n/k 存在无效结点干扰的情况 int turn = order/k;for(i=0,j=0;j<turn;i=i+k,j++)sort(list+i,list+i+k,cmp2);for(i=0;i<order-1;i++)printf("%05d %d %05d\n",list[i].address,list[i].data,list[i+1].address);printf("%05d %d -1\n",list[order-1].address,list[order-1].data);return 0;
}

换了个方法试试,如下:

#include<cstdio>
#include<algorithm>using namespace std;struct node
{int address;int data;int next;int id;
}list[100010],print[100010];bool cmp(node a,node b)
{return a.id>b.id;
}int main()
{int begin,n,k,i,cnt = 0;scanf("%d%d%d",&begin,&n,&k);for(i=0;i<n;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);list[a].address = a;list[a].data = b;list[a].next = c;}//将正确顺序存在另一个结构体中 while(begin!=-1){print[cnt++] = list[begin];print[cnt-1].id = cnt-1;begin = list[begin].next;}int t = 0;//需要reverse时也可以通过设置id,再排序来实现 //注意排序和输出都是cnt不是n,因为题目存在无效结点 for(i=0;i<cnt/k;i++){sort(print+t*k,print+t*k+k,cmp);t++;}for(i=0;i<cnt;i++){printf("%05d %d",print[i].address,print[i].data);if(i!=cnt-1) printf(" %05d\n",print[i+1].address);else printf(" -1\n");}return 0;
}

 

这篇关于PAT 1074 Reversing Linked List [静态链表] [无效结点]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/888040

相关文章

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定