洛谷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();
}
}