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 (≤) which is the total number of nodes, and a positive K (≤) 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:

  1. 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:

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

Sample Output:

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

算法设计:
由于所给的地址是5位非负整数,可以定义两个维度为100005的一维数组data、Next,负责储存数据域和下一个地址
定义一个vector<int>listAddress,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到listAddress中。
按要求对listAddress数组进行翻转,可以利用c语言库里的reverse函数

按格式要求进行结果输出

注意点:

(1)题目给出的结点中可能有不在链表中的无效结点

(2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转

(3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出

  1. //s首先说明一下PAT的常见陷阱,就是给出的数据未必是一条链表,可能是多条,
  2. //所以首先从输入的数据的中找到那条链表
  3. //巨简单,使用vector反转就行了
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8. int head, N, K;
  9. int nodes[], nexts[];
  10. vector<int>list;
  11. int main()
  12. {
  13. cin >> head >> N >> K;
  14. int adrr, data, next, temp = head;
  15. for (int i = ; i < N; ++i)
  16. {
  17. cin >> adrr >> data >> next;
  18. nodes[adrr] = data;
  19. nexts[adrr] = next;
  20. }
  21. while (temp != -)//找出这条链表
  22. {
  23. list.push_back(temp);
  24. temp = nexts[temp];
  25. }
  26. for (int i = K; i <= list.size(); i += K)
  27. reverse(list.begin() + i - K, list.begin() + i);
  28. for (int i = ; i < list.size(); ++i)
  29. {
  30. printf("%05d %d ", list[i], nodes[list[i]]);
  31. if (i < list.size() - )
  32. printf("%05d\n", list[i+]);
  33. else
  34. printf("-1\n");
  35. }
  36. return ;
  37. }

PAT甲级——A1074 Reversing Linked List的更多相关文章

  1. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  2. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

  3. PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  4. A1074. Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  5. PAT Advanced 1074 Reversing Linked List (25) [链表]

    题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...

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

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  7. PAT_A1074#Reversing Linked List

    Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...

  8. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

  9. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

随机推荐

  1. 【转载】C# 开源库大全非常好

    原文地址:http://m.blog.csdn.net/woddle/article/details/37311877 C#开源大全 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMuk ...

  2. scala入门基础学习

    1.Scala基础语法 区分大小写 类名 - 对于所有的类名的第一个字母要大写.如果需要使用几个单词来构成一个类的名称,每个单词的第一个字母要大写. 方法名称 - 所有的方法名称的第一个字母用小写. ...

  3. 用于扩展目标跟踪的笛卡尔B-Spline车辆模型

    (哥廷根大学) 摘要 文章提出了一种表示空间扩展物体轮廓的新方法,该方法适用于采用激光雷达跟踪未知尺寸和方向的车辆.我们在笛卡尔坐标系中使用二次均匀周期的B-Splines直接表示目标的星 - 凸形状 ...

  4. soj114 中位数

    题意:给你一个长度为2n-1的数组A,设Bi是A的1~2i-1的中位数.问打乱A,有多少种不同的B序列? 标程: #include<bits/stdc++.h> using namespa ...

  5. [JZOJ3692] 【SRM 611】ElephantDrinking

    题目 题目大意 我真的不知道怎么用简短的语言表述出来-- 直接看题目吧-- 正解 假设只有左边和上边延伸过来的,那似乎很好办:设\(f_{i,j}\)表示左上方到\((i,j)\)所形成的矩形中,如果 ...

  6. python相关软件安装流程图解——MySQL 8.0.13安装教程(windows 64位)——MYSQL依赖的软件——MYSQL必须的系统DLL插件——MYSQL真正的安装

    https://www.mysql.com/https://www.mysql.com/downloads/https://dev.mysql.com/downloads/windows/https: ...

  7. C语言结构体初始化方法

    早上苏凯童鞋问我这个问题来着,写在这里. 我了解到的C中结构体初始化的方法大概有三种. 如这里我定义了一个结构体: typedef struct node { int x, y; }Node; 第一种 ...

  8. python笔记三

    # 数据读写不一定是文件,也可以在内存中读写 # StringIO就是在内存中读写str from io import StringIO f = StringIO() # 要把str写入StringI ...

  9. Vue项目组织规范

    目录 一.项目结构的核心思想 二.项目目录结构 三.资源路径编译规则 四.index.html 五.build目录 和 config目录 六.public目录 七.static 目录 八.src目录结 ...

  10. DEVO 7E遥控器配对

    先把遥控器上电,并把模型里面的固定ID关闭,放在一旁. 接收器断电,按住CLEAN按钮后上电,会发现接收器慢闪两下后松开. 这时遥控器应该就连上接收器了,这时接收器常亮. 再自行配置固定ID即可.