第一个问题,你的数组是从一开始的,所以z的初始值为1。
第二个问题,在m的for循环中,第二个if前要加上else,否则会多执行导致错误。
```cpp
#include <bits/stdc++.h>
using namespace std;
struct m
{
int fx;
string name;
};
m ers[100010];
int main()
{
int n, m, x, y = 0;
int z = 1;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> ers[i].fx >> ers[i].name;
}
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
if ((ers[z].fx == 0 && x == 0) || (ers[z].fx == 1 && x == 1))
{
z = z - y;
if (z <= 0)
{
z = n + z;
}
}
else if ((ers[z].fx == 1 && x == 0) || (ers[z].fx == 0 && x == 1))
{
z = z + y;
if (z > n)
{
z = z - n;
}
}
}
cout << ers[z].name;
return 0;
}
```
by molsonsama @ 2024-02-01 22:49:36