PAT甲级专题|链表
PAT链表专题
关于PAT甲级的链表问题,主要内容 就是”建立链表“
所以第一步学会模拟链表,pat又不卡时间,这里用vector + 结构体,更简洁
模拟链表的普遍代码
const int maxn = 1e6+10;
struct node{
int address;
int next;
char key;
}nod[maxn];
int head1,n;
vector<node> list1;
cin>>head1>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;//重点1
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){ //重点2
list1.push_back(nod[head1]);
}
学会模拟链表之后,PAT甲级的链表题就都能做了,万变不离其宗,
基本就是,建立链表、按照题意操作链表(链表合并、去重、反转...)、注意判断链表边界(链表有效长度)、输入输出( %05d 来输出地址)
2019秋季PAT 7-2
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100001;
int ad1,ad2,n;
int head1,head2 = 0;
int len1 = 0,len2 = 0;
struct node{
int add,next,key;
}nod[maxn];
vector<node> list1;
vector<node> list2;
vector<node> res;
int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++){
int add,next,key;
cin>>add>>key>>next;
nod[add].add = add;
nod[add].next = next;
nod[add].key = key;
}
for (int p = head1; p != -1; p = nod[p].next)
list1.push_back(nod[p]);
for(int p = head2;p!=-1;p = nod[p].next)
list2.push_back(nod[p]);
len1 = list1.size()-1;
len2 = list2.size()-1;
if(len1 >= 2*len2){
int p = 0,q = len2;
while(q >= 0){
res.push_back(list1[p++]);
res.push_back(list1[p++]);
res.push_back(list2[len2--]);
}
while(p <= len1){
res.push_back(list1[p++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}else{
int p = len1,q = 0;
while(p >= 0){
res.push_back(list2[q++]);
res.push_back(list2[q++]);
res.push_back(list1[p--]);
}
while(q<=len2){
res.push_back(list2[q++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}
for(int i=0;i<res.size();i++){
if(i == res.size()-1) printf("%05d %d -1\n",res[i].add,res[i].key);
else printf("%05d %d %05d\n",res[i].add,res[i].key,res[i].next);
}
return 0;
}
PTA1032
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
struct node{
int address;
int next;
char key;
}nod[maxn];
int head1,head2,n;
bool use[maxn];
vector<node> list1,list2;
int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){
list1.push_back(nod[head1]);
use[head1] = true;
}
for(head2; head2 != -1; head2 = nod[head2].next){
if(use[head2]){
printf("%05d\n",head2);
return 0;
}
}
cout<<"-1"<<endl;
return 0;
}
PTA1052
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;
struct node{
int address;
int key;
int next;
bool flag;
}nod[100010];
vector<node> list1;
int n;
int head;
bool cmp(node a,node b){
return a.key < b.key;
}
int main(){
cin>>n>>head;
for(int i=1;i<=n;i++){
int address,key,next;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
nod[address].flag = true;
}
int m = 0;
for(;head!=-1;head = nod[head].next){
nod[head].flag = true;
list1.push_back(nod[head]);
m++;
}
if(m == 0){ //判边界 否则段错误
printf("0 -1\n");
return 0;
}
sort(list1.begin(),list1.end(),cmp);
for(int i=0;i<m-1;i++){
list1[i].next = list1[i+1].address;
}
cout<<m<<" ";
printf("%05d\n",list1[0].address);
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i].next);
}
printf("%05d %d -1\n",list1[m-1].address,list1[m-1].key);
return 0;
}
PTA1074
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
/*
题目意思是 k个元素间换一下
*/
struct node{
int address;
int key;
int next;
}nod[maxn];
vector<node>list1,list2;
int head,n,k;
void print(){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i].next);
}
printf("%05d %d -1\n",list2[n-1].address,list2[n-1].key);
}
int main(){
cin>>head>>n>>k;
for(int i=0;i<n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
}
for(;head!=-1;head=nod[head].next)
list1.push_back(nod[head]);
n = list1.size(); //原来的n不一定是新链表的长度 可能输入数据有无效结点
int pos = 0;
while(pos + k - 1 < n){ //这里注意 下标 每pos~pos+k-1为一组
for(int i = pos+k-1;i>=pos;i--){ //倒序存进list2
list2.push_back(list1[i]);
}
pos+=k;
}
if(pos < n){ //剩下的没有凑够k个就 直接存入链表list2
for(int i=pos;i<n;i++){//正序存进list2
list2.push_back(list1[i]);
}
}
for(int i=0;i<n-1;i++){ //更新next地址
list2[i].next = list2[i+1].address;
}
list2[n-1].next = -1;
print();
return 0;
}
/*
00100 4 2
00000 4 -1
00100 1 12309
33218 3 00000
12309 2 33218
*/
PTA1097
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int head,n;
struct node{
int address;
int key;
int next;
}nod[maxn];
bool vis[maxn];
vector<node> list1,list2;
int main(){
cin>>head>>n;
for(int i = 0;i < n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
}
for(;head!=-1;head = nod[head].next){
if(!vis[abs(nod[head].key)]){
vis[abs(nod[head].key)] = true;
list1.push_back(nod[head]);
}else{
list2.push_back(nod[head]);
}
}
n = list1.size();
if(n != 0){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i+1].address);
}
printf("%05d %d -1\n",list1[n-1].address,list1[n-1].key);
}
int m = list2.size();
if(m != 0){
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i+1].address);
}
printf("%05d %d -1\n",list2[m-1].address,list2[m-1].key);
}
return 0;
}
PAT甲级专题|链表的更多相关文章
- PAT甲级专题|树的遍历
PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...
- PAT甲级专题|最短路
PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- PAT甲级题分类汇编——理论
本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...
随机推荐
- MIT线性代数:10.4个基本子空间
- 题解【洛谷】CF134A
题解 CF134A [Average Numbers] 这题就是简单的模拟. 只不过要优化一下常数什么的 思路: 为了不浪费时间总是取平均数,直接用一个 S 储存总和,每次都减去 a_i 再除以 n ...
- [ES]Python查询ES导出数据为Excel
版本 elasticsearch==5.5.0 python==3.7 说明 用python查询es上存储的状态数据,将查询到的数据用pandas处理成excel code # -*- coding: ...
- oop面向对象知识总结 静态成员和友元
第十一章 静态成员和友元 11.1 静态成员 1.C++类当中的静态数据成员仍借用保留字static,但是与之前的静态全局变量,静态局部变量以及静态函数没有关系. 2.静态数据成员不占用具体对象的数据 ...
- NOIP模拟 38
liu_runda的题! 错过辽QAQ T1虽然没用题解的损益法,但是用高精%还能过.. 没想到敲完就过编译了,还以为要调一天呢 高精度的阴影没了- T2的思路很巧妙 首先一个区间最多有一种颜色占一半 ...
- 磁盘冗余阵列之RAID10的配置
1988年由加利福尼亚大学伯克利分校发表的文章首次提到并定义了RAID,当今CPU性能每年可提升30%-50%但硬盘仅提升7%,渐渐的已经成为计算机整体性能的瓶颈,并且为了避免硬盘的突然损坏导致数据丢 ...
- python经典面试算法题4.1:如何找出数组中唯一的重复元素
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. [百度面试题] 难度系数:⭐⭐⭐ 考察频率:⭐⭐⭐⭐ 题目描述 ...
- Md5实例
MD5实例 我的md5源码 当我们对数据进行操作时,存储到数据库时,有时候不希望别人能够看到,通过md5能够实现对数据的加密. java代码 ```javaimport org.springframe ...
- html部分知识点(待完善)
html部分知识点 跳转 在A页面某链接处`<a href="#aa" target="_blank"></a> 在B页面指定的标题处定 ...
- 字体图标转base64
如果你在阿里矢量库下载了字体图标在项目引入无法显示时,可以把图标转成base64 在线转换的链接 https://transfonter.org/ css字体图标的制作