PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】
7-2 Merging Linked Lists (25 分)
Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n L1=a1→a2→...→an−1→an and L 2 =b 1 →b 2 →...→b m−1 →b m L2=b1→b2→...→bm−1→bm . If n≥2m n≥2m , you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a 1 →a 2 →b m →a 3 →a 4 →b m−1 ... a1→a2→bm→a3→a4→bm−1... For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.
Input Specification
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L 1 L1 and L 2 L2 , plus a positive N(≤10 5 ) N(≤105) which is the total number of nodes given. 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 a positive integer no more than 10 5 105 , and Next
is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.
Output Specification
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
Sample Output
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
【声明】
由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。
Solution:
很简单,就是先把节点数据存储下来,然后分别获取出两条链表的节点,然后进行按要求拼接。
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
struct Node
{
int addr, val, next;
}nodes[];
int main()
{
int head, head1, head2, n;
cin >> head1 >> head2 >> n;
vector<Node>v1, v2, res;//存储列表值
for (int i = ; i < n; ++i)
{
int a, b, c;
cin >> a >> b >> c;
nodes[a] = { a,b,c };
}
for (int p = head1; p != -; p = nodes[p].next)
v1.push_back(nodes[p]);
for (int p = head2; p != -; p = nodes[p].next)
v2.push_back(nodes[p]);
if (v1.size() > v2.size())
head = head1;
else
{
v2 = v1;//v2是短边
head = head2;
}
int k = ;
while (head != -)
{
res.push_back(nodes[head]);
++k;
if (k % == && !v2.empty())//两个中间插一个
{
res.push_back(v2.back());
v2.pop_back();
}
head = nodes[head].next;
}
for (int i = ; i < res.size() - ; ++i)
printf("%05d %d %05d\n", res[i].addr, res[i].val, res[i+].addr);
printf("%05d %d %d\n", res.back().addr, res.back().val, -);
return ;
}
PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】的更多相关文章
- PAT甲级【2019年3月考题】——A1157 Anniversary【25】
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...
- PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】
7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...
- PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】
7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...
- PAT甲级【2019年9月考题】——A1160 Forever【20】
7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...
- PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...
- PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】
Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
随机推荐
- 57-python基础-python3-集合-集合常用方法-添加元素-add()-update()
添加元素-add()-update() 1-add() add()用于增加一个元素值,原值修改,无返回值. 2-update()用于添加一个可迭代的对象,原值修改,无返回值. 下面依次向集合添加可迭代 ...
- 如何写一个简单的基于 Qt 框架的 HttpServer ?
httpserver.h #ifndef HTTPSERVER_H #define HTTPSERVER_H #include <QObject> #include <QtCore& ...
- k8s入门教程
1. k8s概述 Kubernetes(简称K8S) 是Google开源的分布式的容器管理平台,方便我们在服务器集群中管理我们容器化应用. 教程主要介绍怎么使用阿里云容器服务(kubernetes版本 ...
- dubbo构建应用
1.Dubbo介绍 Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 2.Dubbo原理 是不是看着 ...
- JavaFX程序初次运行创建数据库并执行建表SQL
在我的第一个JavaFX程序完成安装的时候才突然发现,不能要用这个软件还要手动执行Sql来建表吧? 于是我的想法是在Main程序中执行时检测数据库连接状况,如果没有检测到数据库或者连接异常,那么出现错 ...
- Css3-文字
一.text-overflow text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法:text-overflow:clip(默认属性,表示剪切) | ell ...
- Ajax异步请求返回文件流(eg:导出文件时,直接将导出数据用文件流的形式返回客户端供客户下载)
在异步请求中要返回文件流,不能使用JQuery,因为$.ajax,$.post 不支持返回二进制文件流的类型,可以看到下图,dataType只支持xml,json,script,html这几种格式,没 ...
- java随机数Math.random()
double random=Math.random();//返回[0,1)随机数 (int)(Math.random()*6)//返回0-5:随机数 (int)(Math.random()*6+1)/ ...
- HDU4035 Maze 期望DP+树形DP(好题)
题意:有一个树形的迷宫,有N个房间(标号为1~N)以及N-1条通道将它们连通,一开始在1号房间,每进入一个房间i,有k[i]的概率被陷阱杀死回到房间1,有s[i]的概率找到出口逃离迷宫,如果没有找到出 ...
- httpclient get/post请求
public static String httpPost(String url, JSONObject json) { String respContent = null; try{ HttpPos ...