close
題目概要:
找出骰子經過選轉後,正面數字(骰子由上往下看)為多少。
解題方向:
1. 骰子正面位子1(由上往下看)、2(north)、3(west)。每兩相對應的面相加為 7。Ex 1+6=7,1 的正下方為 6 。
2. 骰子向南or北轉,只會變更南or北的數值,東or西不變。東or西轉,則南or北不變。
3. 只需要東西南北跟正面位子(由上往下看)的值即可,東西南北的值可以利用兩相對面值相加等於7。
程式碼:
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; | |
class main{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int cases; //轉的次數。 | |
while(sc.hasNextInt() && (cases=sc.nextInt())!=0){ | |
//初始化。 | |
int currPost=1; | |
int N=2,W=3,S=5,E=4; | |
//進行骰子翻轉。 | |
for(int i=0;i<cases;i++){ | |
String st=sc.next(); | |
if(st.equals("north")){ | |
N=currPost; | |
currPost=S; | |
S=7-N; | |
}else if(st.equals("south")){ | |
S=currPost; | |
currPost=N; | |
N=7-S; | |
}else if(st.equals("east")){ | |
E=currPost; | |
currPost=W; | |
W=7-E; | |
}else if(st.equals("west")){ | |
W=currPost; | |
currPost=E; | |
E=7-W; | |
} | |
} | |
//Output | |
System.out.println(currPost); | |
} | |
} | |
} |
文章標籤
全站熱搜