close
題目概要:
計算出目前位子為多少。如果輸入為 RIGHT 則位子+1,如果輸入為LEFT 則位子-1,如果輸入為 SAME AS ? 則去找輸入第?次是做什麼。
解題方向:
字串判斷即可,只不過當向右走或向左走,則需要陣列紀錄,這樣當輸入為 SAME AS ? 時才能查詢。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
} | |
} |
文章標籤
全站熱搜