题目


in1
7 3
0 singer
0 reader
0 mengbier
1 thinker
1 archer
0 writer
1 mogician
0 3
1 1
0 2
out1
writer
in2
10 10
1 c
0 r
0 p
1 d
1 e
1 m
1 t
1 y
1 u
0 v
1 7
1 1
1 4
0 5
0 3
0 1
1 6
1 2
0 8
0 4
out2
y
思路
环形链表,用mod来避免指向元素的越界即可
顺逆旋转用异或函数处理
代码
#include<iostream>
using namespace std;
struct dot
{
int in_out;
string names;
};
struct circle
{
dot* cir;
int head = 0;
};
int main()
{
int n,m;
cin >> n >> m;
circle c;
c.cir = new dot[n];
for(int i = 0; i < n; i++)
cin >> c.cir[i].in_out >> c.cir[i].names;
for(int i = 0; i < m; i++)
{
int l_r;
cin >> l_r;
int clockwise;
cin >> clockwise;
if(l_r ^ c.cir[c.head].in_out)
{
c.head = (c.head + clockwise + n) % n;
}
else
{
c.head = (c.head - clockwise + n) % n;
}
//cout << c.head << endl;
}
cout << c.cir[c.head].names << endl;
}
本文介绍了一种基于环形链表的旋转算法实现方法,通过使用异或运算巧妙地处理了链表的顺时针与逆时针旋转操作。通过示例代码详细解释了如何根据输入参数调整链表头部指针的位置。
701

被折叠的 条评论
为什么被折叠?



