模拟题练习一

这道题犯得一个小错误,当使用循环队列时,下标最好从0开始

#include 
#include 
using namespace std;
int n, m;

struct Person {
    int oriention;
    string occupation;
}p[100005];

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> p[i].oriention >> p[i].occupation;
    }
    int cur = 0;
    for (int i = 1; i <= m; i++) {
        int a, s;
        cin >> a >> s;
        //向左数
        if (a == 0) {
            //朝向圈内
            if (p[cur].oriention == 0) {
                cur = (cur - s + n) % n;
            }
            else {
                cur = (cur + s) % n;
            }
        }
        //向右数
        else {
            //朝向圈外
            if (p[cur].oriention == 0) {
                cur = (cur + s) % n;
            }
            else {
                cur = (cur - s + n) % n;
            }
        }
    }
    cout << p[cur].occupation;
    return 0;
}

你可能感兴趣的:(算法,c++,数据结构)