java 實現類似C++結構體

P1563 玩具謎題:https://www.luogu.com.cn/problem/P1563

import java.util.*;

class Node{
    String name = "";
    int direction = 0;
    Node(int direction, String name){
        this.name = name;
        this.direction = direction;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();//小人
        int m = scan.nextInt();//指令條數
        Node[] node = new Node[n];
        for (int i = 0; i < n; i ++){
            node[i] = new Node(scan.nextInt(), scan.next());
        }

        int step = 0;
        for (int i = 0; i < m; i ++){
            int ans = scan.nextInt();
            int num = scan.nextInt();
            if ((ans == 0 && node[step].direction == 0) || (ans == 1 && node[step].direction == 1))
                step = (step + n - num) % n;
            else if ((ans == 0 && node[step].direction == 1) || (ans == 1 && node[step].direction == 0))
                step = (step + num + n) % n;
        }
        System.out.print(node[step].name);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章