玩具谜题

首先这道题就是一个大模拟,只需要简单的优化即可。

不开long long见祖宗。

优化:朝外向左与朝内向右是一样的方向,反之亦然,因此模拟判断过程。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define scy(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout);
using namespace std;
inline int read(){
  int x=0,f=1;
  char ch=getchar();
  while(ch>'9'||ch<'0'){
    if(ch=='-') f=-1;
    ch=getchar();
  }
  while(ch>='0'&&ch<='9'){
    x=(x<<1)+(x<<3)+(ch^48);
    ch=getchar();
  }
  return x*f;
}
struct node
{
    int head;
    string name;
}a[100005];
int n,m,x,y;
int main()
{
  //scy("in");
    n=read(),m=read();
    for(int i=0;i<n;i++)
    {
      a[i].head=read(),cin>>a[i].name;
    }
    int now=0;
    for(int i=1;i<=m;i++)
    {
        x=read(),y=read();
        if(a[now].head==0&&x==0)now=(now+n-y)%n;
        else if(a[now].head==0&&x==1)now=(now+y)%n;
        else if(a[now].head==1&&x==0)now=(now+y)%n;
        else if(a[now].head==1&&x==1)now=(now+n-y)%n;
    }
    cout<<a[now].name<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/scy-fisheep/p/13804552.html