刷题记录:牛客NC16426[NOIP2016]玩具谜题

传送门:牛客

小南希望你写程序帮他解决类似的谜题。这样的谜题具体可以描述为:
有 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值