Under my umbrella.

SKII

Less is more.

洛谷 P1563 玩具谜题 (C/C++, JAVA)

洛谷 P1563 玩具谜题

/*
  P1563 玩具谜题
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
struct node 
{
    int head;
    string name;
}a[100005];
int n,m,x,y;
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].head>>a[i].name;
    }
    int now=0;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y;
        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;
}
import java.util.Scanner;
class Node {
    static int cnt = 0;
    public int direction = 0;
    public String name = "";
    Node(int a, String b) {
        cnt++;
        direction = a; name = b;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(), m = scan.nextInt();
        Node[] toys = new Node[n];
        for(int i = 0; i < n; i++) {
            toys[i] = new Node(scan.nextInt(), scan.next());
        }
        int pos = 0;
        for(int i = 0; i < m; i++) {
            int a = scan.nextInt(), s = scan.nextInt();
            if(a == 0 && toys[pos].direction == 0) {
                pos = (pos + n - s) % n;
            } else if (a == 0 && toys[pos].direction != 0) {
                pos = (pos + s) % n;
            } else if(a == 1 && toys[pos].direction == 0) {
                pos = (pos + s) % n;
            } else {
                pos = (pos + n - s) % n;
            }
        }
        System.out.println(toys[pos].name);
    }
}
posted @ 2020-01-20 17:42  NLYang  阅读(175)  评论(0编辑  收藏  举报