[PAT B1025/A1074]Reversing Linked List

[PAT B1025/A1074]Reversing Linked List

本题是PAT乙级1025题和甲级1074题,这里先放英文原题,想要做乙级题的同学可以直接看后面的中文原题

题目描述

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.

输入格式

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.

输出格式

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.

输入样例

00100 6 4

00000 4 99999

00100 1 12309

68237 6 -1

33218 3 00000

99999 5 68237

12309 2 33218

 

 

输出样例

00000 4 33218

33218 3 12309

12309 2 00100

00100 1 99999

99999 5 68237

68237 6 -1

---------------------------------------中文原题---------------------------------------------

题目描述

给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。

输入格式

每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤10^{5}​​)、以及正整数 K (≤N),即要求反转的子链结点的个数。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。

接下来有 N 行,每行格式为:

Address Data Next

其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。

输出格式

对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

解析

这道题目我算是debug了很久了,按照晴神的思路来写的(最近真的是觉得自己越来越菜,很多题目自己写总是拿不了满分,或者老是出错,可能还是不太熟练吧),注意题目挖的坑,首先n的值是会变的,这里的条件很隐晦,因为题目并没有说输入的点是否有效,所以n个点并不见得都有效,而我们只能对有效的点进行处理,所以这里要注意。另外就是晴神是使用了下标之间的关系来输出的,实际上并没有反转链表,所以其实我们本可以使用reverse函数进行处理的,这样会来得更加快,也更加简单,有兴趣的读者可以自己尝试。

这题目用到的主要就是静态链表,对于地址格式非常整齐而且范围又不大的链表,我们可以使用静态链表来实现,另外该题目还需要注意的地方就是输出的时候要以%05d的格式输出对齐,否则会出错。而这样又会导致一个问题就是,-1按照%05d的个数输出会出错,所以又需要讨论。

#include<iostream>
#include<algorithm>
using namespace std;
struct Node {
	int addr, num, next;  //某个元素的地址和存储的值和他的下一个点的地址
	int order;  //存放某个点在链表中的位置,后面根据它来排序
};
Node nodes[100010];  //静态链表
bool cmp(Node n1, Node n2) {
	return n1.order < n2.order; //自己写cmp函数,实现排序
}
int main()
{
	int begin, n, k;
	scanf("%d%d%d", &begin, &n, &k);
	for (int i = 0; i < 100010; i++) nodes[i].order = 99999; //初始化order都为最大值
	for (int i = 0; i < n; i++) {
		int addr, num, next;
		scanf("%d%d%d", &addr, &num, &next);
		nodes[addr].addr = addr;
		nodes[addr].num = num;
		nodes[addr].next = next;
	}
	int cnt = 0;              //这个用来记录有效元素的个数
	while (begin != -1) {
		nodes[begin].order = cnt++;
		begin = nodes[begin].next;
	}
	n = cnt;           //这里我们令n=cnt,因为n个元素有效的元素只有cnt个
	sort(nodes, nodes + 100010, cmp);
	int group = n / k;  //有多少个子链表要进行逆转
	for (int i = 0; i < group; i++) {
		for (int j = (i + 1)* k - 1; j > i * k; j--) {
			if (nodes[j].next != -1) printf("%05d %d %05d\n", nodes[j].addr, nodes[j].num, nodes[j - 1].addr);
			else printf("%05d %d %d\n", nodes[j].addr, nodes[j].num, nodes[j - 1].addr);
		}
		if (i < group - 1) printf("%05d %d %05d\n", nodes[i*k].addr, nodes[i*k].num, nodes[(i + 2)*k - 1].addr);
		else {
			if (nodes[(i + 1)*k - 1].next == -1) printf("%05d %d -1\n", nodes[i*k].addr, nodes[i*k].num);
			else {
				printf("%05d %d %05d\n", nodes[i*k].addr, nodes[i*k].num, nodes[(i + 1)*k - 1].next);
				for (int i = k * group; i < n; i++) {
					if (nodes[i].next != -1) printf("%05d %d %05d\n", nodes[i].addr, nodes[i].num, nodes[i].next);
					else printf("%05d %d %d\n", nodes[i].addr, nodes[i].num, nodes[i].next);
				}
			}
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值