2019年9月8日秋季PAT甲級題解-2-1161-Merging Linked Lists (25 分)
Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→bm. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like 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 L1 and L2, plus a positive N (≤10^5) 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 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
連結串列等於不難
將l1設為長得,l2設為短的。然後就是輸出比較難了,通過add作為下標的就是靜態連結串列,輸出只要找對下一個結點是啥,輸出它的地址就行,不用改自己的next
分三種情況討論:
//每隔兩個長的就輸出一個短的,記得最後一位要單獨處理
//最後一位如何處理?
//3種情況,1.是短的,判斷長的最後一位已經輸出了,短的是自己的最後一位,就輸出-1
//2.是第一位的長的,判斷此時短的是否輸出完了,且長的是否是自己的最後一位,就輸出-1
//3.是第二位的長的,判斷此時短的是否輸出完了,且長的是否是自己的最後一位,就輸出-1
//每隔兩個插入一個
//靜態連結串列,搞多個vector分別放即可
//不需要修改next,只需要直接輸出下一位該輸出的add即可
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
int add;
int val;
int next;
}nodes[100000];
int main(){
int s1,s2,n;
cin>>s1>>s2>>n;
int add,val,next;
vector<node> l1;
vector<node> l2;
for(int i = 0; i < n; i++){
cin>>add>>val>>next;
nodes[add] = {add,val,next};
}
for(int i = s1; i != -1; i = nodes[i].next){
l1.push_back(nodes[i]);
}
for(int i = s2; i!=-1; i = nodes[i].next){
l2.push_back(nodes[i]);
}
vector<node> temp;
if(l1.size() > l2.size()){
reverse(l2.begin(),l2.end());
}
else if(l1.size() < l2.size()){
temp = l1;
l1 = l2;
l2 = temp;
reverse(l2.begin(),l2.end());
}
//每隔兩個長的就輸出一個短的,記得最後一位要單獨處理
//最後一位如何處理?
//3種情況,1.是短的,判斷長的最後一位已經輸出了,短的是自己的最後一位,就輸出-1
//2.是第一位的長的,判斷此時短的是否輸出完了,且長的是否是自己的最後一位,就輸出-1
//3.是第二位的長的,判斷此時短的是否輸出完了,且長的是否是自己的最後一位,就輸出-1
int index = 0;
for(int i = 0; i < l1.size(); i++){
if(i%2 == 0){
if(index == l2.size() && i == l1.size()-1) printf("%05d %d -1",l1[i].add,l1[i].val);
else printf("%05d %d %05d\n",l1[i].add,l1[i].val,l1[i+1].add);
}
else{
if(index == l2.size() && i == l1.size()-1){
printf("%05d %d -1",l1[i].add,l1[i].val);//長的作為最後一位
break;
}
else printf("%05d %d %05d\n",l1[i].add,l1[i].val,l2[index].add);
if(i == l1.size() -1 && index == l2.size() -1) printf("%05d %d -1",l2[index].add,l2[index].val);
else printf("%05d %d %05d\n",l2[index].add,l2[index].val,l1[i+1].add);
index++;
}
}
return 0;
}
相關文章
- 2024 秋季PAT認證甲級(題解A1-A4)
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- 2020年7月第2題 PAT甲級真題 The Judger (25分)
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- PAT甲級考試題庫題目分類
- PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk (20分)
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- PAT甲級1032 Sharing
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級1023 Have Fun with Number
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- Leetcode 160. Intersection of Two Linked ListsLeetCode
- PAT甲級1126~1130|C++實現C++
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PAT 1033 To Fill or Not to Fill (25分) 貪心思想
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- 02-線性結構3 Reversing Linked List (25分)
- PAT (Advanced Level) Practice 1149 Dangerous Goods Packaging (25分)Go
- 2021.9.12週六PAT甲級考試覆盤與總結
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- PAT-A1119 題解
- PAT乙級 | 1086 就不告訴你 (15分)
- PAT:1020. Tree Traversals (25) AC
- PAT 乙級
- PTA甲級——Be Unique
- 07-25 題解
- PAT 乙級 1094 谷歌的招聘 (20分)---【素數 字串】谷歌字串
- PAT乙級1023