原题链接
思路:
依旧是很简单的模拟,不过这题有点绕,需要注意以下几点:
- “小南发现, 这个谜题中玩具小人的朝向非常关键, 因为朝内和朝外的玩具小人的左右方向是相反的: 面朝圈内的玩具小人, 它的左边是顺时针方向, 右边是逆时针方向; 而面向圈外的玩具小人, 它的左边是逆时针方向, 右边是顺时针方向。”
- “每行包含一个整数和一个字符串,以逆时针为顺序给出每个玩具小人的朝向和职业。其中0表示朝向圈内,1表示朝向圈外。 ”
- “接下来m行,其中第i行包含两个整数 ai,si,表示第i条指令。若 ai=0表示向左数si个人;若ai=1表示向右数si个人。”
- 玩具小人是站成一个圈。
接下来就是按照题意,理清思路,当执行指令时,先判断ai,再判断当前位置玩具小人朝向。
代码:
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<unordered_map>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<cmath>
#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define pf printf
#define sf scanf
#define sfd(n) scanf("%d", &n)
#define sfll(n) scanf("%lld", &n)
#define pfd(n) printf("%d", n)
#define pfll(n) printf("%lld", n)
#define pft printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define PI 3.1415926
#define MAX 100000
#define M 100003
using namespace std;
int n, m;
typedef struct Toy{
int direction;
string name;
}Toy;
void right(int &pos, int s) {
pos += s;
if( pos>=n ) {
pos %= n;
}
}
void left(int &pos, int s) {
pos -= s;
if( pos<0 ) {
pos %= n;
pos += n;
}
}
int main() {
sfd(n);
sfd(m);
Toy* p = new Toy[n];
for( int i=0; i<n; i++ ) {
cin >> p[i].direction;
getchar();
cin >> p[i].name;
}
int a, s;
int pos = 0;
for( int i=0; i<m; i++ ) {
sfd(a);
sfd(s);
if( a==0 ) {
if( p[pos].direction==0 ) {
left(pos, s);
}else {
right(pos, s);
}
}else {
if( p[pos].direction==0 ) {
right(pos, s);
}else {
left(pos, s);
}
}
}
cout << p[pos].name;
return 0;
}