嗯...
题目链接:https://www.luogu.org/problem/P1563
这道题主要问题就是弄明白顺逆时针的问题,其实可以简化成一个异或的问题:
当head与x异或值为零时,即为顺时针;否则为逆时针。
其次要注意顺逆时针的处理:
顺:now = (now + y) % n; 逆:now = (now + n - y) % n;
细节:a[]的下标要从0开始,因为在%操作中余数会出现0的情况
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 struct node{ 7 int head; 8 string name; 9 } a[100005]; 10 11 int main(){ 12 int n, m; 13 scanf("%d%d", &n, &m); 14 for(int i = 0; i < n; i++) 15 cin >> a[i].head >> a[i].name; 16 int now = 0; 17 for(int i = 1; i <= m; i++){ 18 int x, y; 19 scanf("%d%d", &x, &y); 20 if(x ^ a[now].head) now = (now + y) % n; 21 else now = (now + n - y) % n; 22 } 23 cout << a[now].name; 24 return 0; 25 }