close

題目概要:

計算出目前位子為多少。如果輸入為 RIGHT 則位子+1,如果輸入為LEFT 則位子-1,如果輸入為 SAME AS ? 則去找輸入第?次是做什麼。

解題方向:

字串判斷即可,只不過當向右走或向左走,則需要陣列紀錄,這樣當輸入為 SAME AS ? 時才能查詢。

程式碼:

//Java
import java.util.Scanner;
import java.util.Vector;
class uva12503{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=Integer.parseInt(sc.nextLine()); //總共有多少case。
for(int i=0;i<cases;i++){
Vector<String> vector=new Vector<String>(); //紀錄走的方向。
int p=0; //目前位子。
int n=Integer.parseInt(sc.nextLine()); //需要走的次數。
//開始走。
for(int j=0;j<n;j++){
String st=sc.nextLine();
if(st.equals("RIGHT")){
vector.add("RIGHT");
p++;
}else if(st.equals("LEFT")){
vector.add("LEFT");
p--;
}else{
int num=0;
for(int k=0;k<st.length();k++)if(st.charAt(k)>=48 && st.charAt(k)<=57)num=num*10+st.charAt(k)-48;
if(vector.get(num-1).equals("RIGHT")){
vector.add("RIGHT");
p++;
}else{
vector.add("LEFT");
p--;
}
}
}
//Output。
System.out.println(p);
}
}
}
view raw uva12503.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()