P1563 [NOIP2016 提高组] 玩具谜题

在这里插入图片描述
先分析一下这个题目:
1.当小人朝外的时候,他的右边就是顺时针,他的左边就是逆时针。
2.当小人朝内的时候,他的左边时顺时针,他的右边时逆时针。
可以总结一下:
假设朝外为1,朝内为0;右边为1,左边为0.
则有(朝向:方向)

朝向:方向顺逆
1:0逆时针
1:1顺时针
0:0顺时针
0:1逆时针
很明显找到规律,当朝向和方向一致的时候顺时针,否则逆时针.

先放一个没有优化的代码,满分ac,但是写的不好,用的循环队列写的

#include<stdio.h>
#pragma warning (disable:4996)
struct people
{
	int face;
	char name[11];
}people[100000];

struct act
{
	int dir;
	long long int move;
}act[100000];

int main()
{
	int n, m;
	int str = 0;  //伪指针
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d %s", &people[i].face, people[i].name);
	for (int i = 0; i < m; i++)
		scanf("%d%d", &act[i].dir, &act[i].move);
	for (int i = 0; i < m; i++)
	{
		long long int move = act[i].move;
		if (act[i].dir == people[str].face)  //方向和朝向相同,顺时针,即(朝向外侧的右边等于朝向内侧的左边)
		{
			move = move - move / n * n;
			str = (n + str - move) % n;
		}
		else   //方向和朝向不同,逆时针
		{
			str = (str + move) % n;
		}
	}
	printf("%s", people[str].name);
	return 0;
}

在这里插入图片描述
下面是优化的代码:

#include<stdio.h>
struct people
{
	int face;
	char name[11];
}people[100000];

int main()
{
	int n, m;
	int dir, move;  //朝向,移动的步数
	int str = 0;    //str最后指向拿帽子人
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d%s", &people[i].face, people[i].name);
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d", &dir, &move);
		if (people[str].face == dir)   //朝向一样,,顺时针
			str = (str + n - move) % n;
		else   //朝向不一样,逆时针
			str = (str+n+move) % n;
	}
	printf("%s", people[str].name);
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值