#43746: java答案


w122322222@gmail.com (m n j h p)

學校 : 不指定學校
編號 : 289597
來源 : [140.136.24.78]
最後登入時間 :
2024-11-13 17:12:43
e567. 12503 - Robot Instructions -- UVA | From: [140.136.20.201] | 發表日期 : 2024-10-27 17:20

java參考答案
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

public static int same(List<String> list, int x, int y) {
String command = list.get(y);

if (command.equals("LEFT")) {
x -= 1;
} else if (command.equals("RIGHT")) {
x += 1;
} else if (command.contains("SAME AS")) {
int index = Integer.parseInt(command.substring(8)) - 1;
x = same(list, x, index);
}

return x;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();

for (int i = 0; i < count; i++) {
int code_count = sc.nextInt();
sc.nextLine();

List<String> list = new ArrayList<>();
for (int n = 0; n < code_count; n++) {
String command = sc.nextLine();
list.add(command);
}

int x = 0;
for (int p = 0; p < list.size(); p++) {
String command = list.get(p);
if (command.equals("LEFT")) {
x -= 1;
} else if (command.equals("RIGHT")) {
x += 1;
} else if (command.contains("SAME AS")) {
int index = Integer.parseInt(command.substring(8)) - 1;
x = same(list, x, index);
}
}
System.out.println(x);
}

sc.close();
}
}
 
ZeroJudge Forum