当前位置:网站首页>L2-022 重排链表 (25 分)(map+结构体模拟)
L2-022 重排链表 (25 分)(map+结构体模拟)
2022-04-23 08:43:00 【.Ashy.】
描述:
输入:
每个输入包含1个测试用例.每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N ≤105)。结点的地址是5位非负整数,NULL地址用−1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址;Data是该结点保存的数据,为不超过10 5
的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。
输出:
对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。
样例输入:
00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
样例输出:
68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1
注意:
测试点三一开始错了,试了试,发现给出的地址数据中有不在链表中的无效数据,改正一下;
/* 00100 6 00000 4 99999 00100 1 12309 33218 3 00000 99999 5 -1 12309 2 33218 sssss 6 sssss//无效数据测试 */
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll maxx = 1e18;
const int N = 1e6+100;
const int ps = 1e4+10;
const double eps = 1e-8;
string s1,ss;
int n,cnt;
pair<int,string>p;
map<string,pair<int,string>>mp;
struct node{
string adds;
int k;
}a[N];
int main()
{
cin>>s1>>n;
for(int i=1;i<=n;i++)
{
cin>>ss>>p.first>>p.second;
mp[ss]=p;
}//记录信息
while(s1!="-1")
{
a[++cnt].adds=s1;
a[cnt].k=mp[s1].first;
s1=mp[s1].second;
}//把信息存到结构体中
n=cnt;//把 n 换成有效个数
if(n%2==0)
{
for(int i=1;i<=n/2;i++)
{
cout<<a[n-i+1].adds<<" "<<a[n-i+1].k<<" ";
cout<<a[i].adds<<endl<<a[i].adds<<" "<<a[i].k<<" ";
if(i!=n/2)
cout<<a[n-i].adds<<endl;
else
cout<<"-1";
}
}
else
{
for(int i=1;i<=n/2;i++)
{
cout<<a[n-i+1].adds<<" "<<a[n-i+1].k<<" ";
cout<<a[i].adds<<endl<<a[i].adds<<" "<<a[i].k<<" ";
cout<<a[n-i].adds<<endl;
}
cout<<a[n/2+1].adds<<" "<<a[n/2+1].k<<" "<<"-1";
}
}//依次输出
版权声明
本文为[.Ashy.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/woshilichunyang/article/details/124357315
边栏推荐
猜你喜欢
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
面了一圈,整理了这套面试题。。
LaTeX论文排版操作
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
K210学习笔记(二) K210与STM32进行串口通信
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
测试你的机器学习流水线
根据字节码获取类的绝对路径
随机推荐
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
Misunderstanding of flush () method of OutputStream class
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Shell script advanced
DJ音乐管理软件Pioneer DJ rekordbox
Detailed description of self feeling of auricular point weight loss 0422
LaTeX数学公式
Kubernetes如何使用harbor拉去私有镜像
企业微信应用授权/静默登录
Shell脚本进阶
计算神经网络推理时间的正确方法
虚拟线上展会-线上vr展馆实现24h沉浸式看展
Go语言自学系列 | golang结构体指针
After a circle, I sorted out this set of interview questions..
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
2021李宏毅机器学习之Adaptive Learning Rate
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
Protobuf简介
Reference passing 1