(Java洛谷)玩具谜题

这篇博客介绍了如何解决洛谷P1563中的玩具谜题问题,主要思路是通过Java代码模拟寻找眼镜的过程,利用异或操作判断小人的移动方向。内容包括原题题意解释和ACJava代码展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷P1563 [NOIP2016 提高组] 玩具谜题


原题题意


思路

本题主要是模拟寻找眼镜的过程,通过给出的输入可以明显的发现其中的规律:当小人的朝向和指令的方向异或为0时,就倒着找,如果异或为1,则正着找。

在java中,异或的表示方法为 ^ .

例如:

int n = 0 ^ 1;
//结果为1

ACjava代码

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        
        
        // Solution
        int flag = 0;
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] token = new int[n];
        String[] s = new String[n];
        int[][] test = new int[m][2];
        for(int i = 0; i < n; i++) {
            token[i] = sc.nextInt();
            s[i] = sc.next();
        }
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < 2; j++) {
                test[i][j] = sc.nextInt();
            }
            int tmp1 = token[flag] ^ test[i][0];
            if(tmp1 == 0) {// 0 0 || 1 1
                flag = (flag+n-test[i][1]) % n;
            }else {
                flag = (flag+test[i][1]) % n;
            }
        }
        out.print(s[flag]);
        
        out.flush();
        
        
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值