2010年7月11日 星期日

Problem 10409 Die Game,骰子遊戲

首先宣告一結構,紀錄骰子上下東西南北所屬數字為何:
struct Dice
{
int up, down, north, east, south, west;
};
struct Dice d;
主程式內,讀入翻動次數 n 次後,先初始化骰子數字各在哪些方位:
d.up = 1, d.down = 6, d.north = 2,
d.east = 4, d.south = 5, d.west = 3;
最後只要依照相對面的骰子總和為 7 以及空間概念,就可寫出來:
while (n --)
{
scanf("%s", str);
if (str[0] == 's') /* 南 */
d.south = d.up, d.up = d.north,
d.down = 7 - d.up, d.north = 7 - d.south;
if (str[0] == 'n') /* 北 */
d.down = d.north, d.north = d.up,
d.up = 7 - d.down, d.south = 7 - d.north;
if (str[0] == 'w')
d.down = d.west, d.west = d.up,
d.up = 7 - d.down, d.east = 7 - d.west;
if (str[0] == 'e')
d.east = d.up, d.up = d.west,
d.down = 7 - d.up, d.west = 7 - d.east;
}
printf("%d\n", d.up);

By David.K

p10409題目連結
回ACM題庫目錄
回首頁

沒有留言: