传送门:牛客
小南希望你写程序帮他解决类似的谜题。这样的谜题具体可以描述为:
有 n 个玩具小人围成一圈,已知它们的职业和朝向。现在第 1 个玩具小人告诉小南一个包含 m 条指令的谜题。其中第
i 条指令形如「左数/右数第 si 个玩具小人」。你需要输出依次数完这些指令后,到达的玩具小人的职业。
输入:
7 3
0 singer
0 reader
0 mengbier
1 thinker
1 archer
0 writer
1 mogician
0 3
1 1
0 2
输出:
writer
提高组的一道题是一道比较简单的模拟,洛谷上评级为普及-
主要思路:根据朝向判断每一个指令要走的方向即可(有点约瑟夫环的意味,注意加一减一
的操作即可)
因为是逆时针方向排列,方向若朝内,往左走,则是标号增加,若朝外,则标号减少
方向若朝外,类似操作(稍微画一下就会明了)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x = 0, w = 1;
char ch = getchar();
for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') w = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x * w;
}
#define maxn 1000000
int n, m;
struct Peo {
int chaxiang;
string name;
} peo[100005];
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
scanf("%d", &peo[i].chaxiang);
cin >> peo[i].name;
}
int fangxiang, renshu;
int pos = 1;
for (int i = 1; i <= m; i++) {
fangxiang = read();
renshu = read();
if (fangxiang == 0) {
if (peo[pos].chaxiang == 1) { //表示向外且往左走,逆时针i+
pos = (pos + renshu - 1) % n + 1;
} else {//表示向外且往右走,顺时针,i-
pos = (pos + n - renshu - 1) % n + 1;
}
} else {
if (peo[pos].chaxiang == 1) {//表示向内且往左走,顺时针i-
pos = (pos + n - renshu - 1) % n + 1;
} else {//表示向内且往右走,逆时针i+
pos = (pos + renshu - 1) % n + 1;
}
}
}
cout << peo[pos].name << endl;
return 0;
}