专栏文章

题解:UVA10409 Die Game

UVA10409题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miq7ts02
此快照首次捕获于
2025/12/04 00:22
3 个月前
此快照最后确认于
2025/12/04 00:22
3 个月前
查看原文

题解

UVA10409


人话描述。

骰子每次朝一个方向滚动时,各个面的数字会像“接力赛”一样交换位置。比如:
  • 向北滚:顶面的数字会传给北面,北面传给底面,底面传给南面,南面再传给顶面。
  • 其他方向类似,但传递顺序不同。
你的目标:记录骰子每次翻滚后的状态,最终揭晓顶面的数字!

解题思路:

骰子的“数字舞蹈”
骰子的每次翻滚都是一次“数字舞蹈”,我们只需记录每个面的数字如何传递。想象骰子的六个面是六个朋友,每次滚动时他们交换位置:
  1. 向北滚:
  • 顶面的朋友 A 跑去北面。
  • 北面的朋友 B 跑到底面。
  • 底面的朋友 C 跑到南面。
  • 南面的朋友 D 跳回顶面。
  1. 向东滚:
  • 顶面的朋友 A 跑去东面。
  • 东面的朋友 B 跑到底面。
  • 底面的朋友 C 跑到西面。
  • 西面的朋友 D 跳回顶面。
Tips:每次滚动只需更新个相关面的位置,其他面保持不动!

代码实现:让骰子“动”起来。

CPP
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;
    while(cin >> n && n != 0)
    { // 直到输入0才结束
        // 骰子的六个面:顶、底、北、南、西、东
        int top = 1, bottom = 6, north = 2, south = 5, west = 3, east = 4;
        
        for(int i = 0; i < n; ++i)
        {
            string cmd;
            cin >> cmd;
            
            if(cmd == "north")
            {
                // 北滚:顶←南,南←底,底←北,北←顶
                int new_top = south;
                south = bottom;
                bottom = north;
                north = top;
                top = new_top;
            }
            else if(cmd == "south")
            {
                // 南滚:顶←北,北←底,底←南,南←顶
                int new_top = north;
                north = bottom;
                bottom = south;
                south = top;
                top = new_top;
            }
            else if(cmd == "east")
            {
                // 东滚:顶←西,西←底,底←东,东←顶
                int new_top = west;
                west = bottom;
                bottom = east;
                east = top;
                top = new_top;
            }
            else if(cmd == "west")
            {
                // 西滚:顶←东,东←底,底←西,西←顶
                int new_top = east;
                east = bottom;
                bottom = west;
                west = top;
                top = new_top;
            }
        }
        cout << top << endl; // 揭晓顶面数字!
    }
    return 0;
}

AC Recode


评论

0 条评论,欢迎与作者交流。

正在加载评论...