洛谷刷题贴

P1563 [NOIP2016 提高组] 玩具谜题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

此处可以写四个判断来模拟四种朝向加向左向右的情况。

(1)、A:朝内,向左(2)、A:朝外,向左 (3)、A:朝内,向左 (4)、A:朝外,向右

最后,附上代码

#include<iostream>
#include<cstring> 
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
struct peo{
	int head;
	string name;
}a[100010]; //定义结构体来存储每个人的朝向和姓名。

int main(){
	
	cin >> n >> m;
	
	for(int i = 1;i<=n;i++) cin >> a[i].head >> a[i].name; //依次输入信息。
	
	int pos  = 1; //代表当前位置
	
	for(int j =1;j<=m;j++){
		int x,y;		
		cin >> x >> y;
		if(a[pos].head == 0 && x == 0 ){
			if(((pos-y+n)%n) == 0){
				pos = n;
			}
			else{
				pos = (pos-y+n)%n;
			}

            //此处要特判pos取模后等于零。
		}
		else if(a[pos].head == 0 && x == 1){
			if(((pos+y)%n) == 0){
				pos = pos+y;
			}
			else{
				pos = (pos+y)%n;
			}
		}
		else if(a[pos].head == 1 && x == 0){
			if(((pos+y)%n) == 0){
				pos = pos+y;
			}
			else{
				pos = (pos+y)%n;
			}	
		} 
		else if(a[pos].head == 1 && x == 1){
			if(((pos-y+n)%n) == 0){
				pos = n;
			}
			else{
				pos = (pos-y+n)%n;
			}
		}
	}		 

	cout << a[pos].name << endl; 
	return 0;

}

注:本人弱媾,大佬勿喷。

  • 28
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值