題目描述
小南有一套可愛的玩具小人,它們各有不同的職業。
有一天,這些玩具小人把小南的眼鏡藏了起來。小南發現玩具小人們圍成了一個圈,它們有的面朝圈內,有的面朝圈外,如下圖:
這時 singer 告訴小南一個謎題:“眼鏡藏在我左數第 3 個玩具小人的右數第 1 個玩具小人的左數第 2 個玩具小人那裏。”
小南發現,這個謎題中玩具小人的朝向非常關鍵, 因爲朝內和朝外的玩具小人的左右方向是相反的:面朝圈內的玩具小人,它的左邊是順時針方向,右邊是逆時針方向;而面向圈外的玩具小人,它的左邊是逆時針方向,右邊是順時針方向。
小南一邊艱難地辨認着玩具小人,一邊數着:
“singer 朝內,左數第 3 個是 archer。”
“archer 朝外,右數第 1 個是 thinker。”
“thinker 朝外,左數第 2 個是 writer。”
“所以眼鏡藏在 writer 這裏!”
雖然成功找回了眼鏡,但小南並沒有放心。如果下次有更多的玩具小人藏他的眼鏡,或是謎題的長度更長,他可能就無法找到眼鏡了。所以小南希望你寫程序幫他解決類似的謎題。這樣的謎題具體可以描述爲:
有 n個玩具小人圍成一圈,已知它們的職業和朝向。現在第 1 個玩具小人告訴小南一個包含 m 條指令的謎題,其中第 i條指令形如“左數/右數第 s_i個玩具小人”。你需要輸出依次數完這些指令後,到達的玩具小人的職業。
輸入描述
輸入的第一行包含兩個正整數 n,m,表示玩具小人的個數和指令的條數。
接下來 n 行,每行包含一個整數和一個字符串,以逆時針爲順序給出每個玩具小人的朝向和職業。其中 0 表示朝向圈內,1 表示朝向圈外。保證不會出現其他的數。字符串長度不超過 10 且僅由小寫字母構成,字符串不爲空,並且字符串兩兩不同。整數和字符串之間用一個空格隔開。
接下來 m 行,其中第 ii 行包含兩個整數 a_i,s_i,表示第 i 條指令。若 a_i=0,表示向左數 s_i個人;若 a_i=1,表示向右數 s_i 個人。保證 a_i 不會出現其他的數 1≤s i≤n。
輸出描述
輸出一個字符串,表示從第一個讀入的小人開始,依次數完 m 條指令後到達的小人的職業。
樣例輸入 1
7 3
0 singer
0 reader
0 mengbier
1 thinker
1 archer
0 writer
1 mogician
0 3
1 1
0 2
樣例輸出 1
writer
樣例輸入 2
10 10
1 c
0 r
0 P
1 d
1 e
1 m
1 t
1 y
1 u
0 v
1 7
1 1
1 4
0 5
0 3
0 1
1 6
1 2
0 8
0 4
樣例輸出 2
y
我的代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static int nextInt()throws IOException{
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args)throws IOException{
// TODO Auto-generated method stub
Scanner read = new Scanner(System.in);
int n = read.nextInt();
int m = read.nextInt();
//System.out.println(n+""+m);
read.nextLine();
String[][] str = new String[n][2];
for(int i=0;i<n;i++)
{
str[i] = read.nextLine().split(" ");
}
/*for(int i=0;i<n;i++)
{
System.out.println(str[i][0]+str[i][1]);
}*/
int ans = 0;//初始的小人
for(int i=0;i<m;i++)
{
int direction = read.nextInt();//方向
int num = read.nextInt();//人數
if(direction == Integer.valueOf(str[ans][0]))
{
ans = (ans-num+n)%n;
}
else
{
ans = (ans+n+num)%n;
}
}
System.out.println(str[ans][1]);
read.close();
}
}
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main {
static int place, n, m;// place是當前位置,n是人物個數,m是指令個數
static int[] N;// 存儲人物朝向的數組
static String[] Name;// 存儲人物名稱的數組
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
n = in.nextInt();
m = in.nextInt();
N = new int[n];
Name = new String[n];
for (int i = 0; i < n; i++) {// 讀取所有的人物
N[i] = in.nextInt();
Name[i] = in.next();
}
for (int i = 0; i < m; i++) {// 讀取所有操作同時計算
opeart(in.nextInt(), in.nextInt());
}
System.out.println(Name[place]);
}
static void opeart(int i, int j) {
// 其實只有兩種情況,
// 1 如果朝向內側又向左移動表示爲 0 0,如果朝向外側有向右移動表示爲1 1,這種情況下應該用當前位置減去移動的距離
// 2朝向和移動方向不想同0 1或者1 0,則應該用當前位置加上移動的距離
if (N[place] == i)
place = (place + n - j) % n;
else
place = (place + j) % n;
}
// 這是個內部類,借鑑cf上某個java大佬的寫法,只是爲了解決讀取輸入太慢的問題
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}