投掷骰子问题(骰子是一个立方体,每个面一个数字,初始为左1,右2,前3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Aoulun/article/details/80137914

题目描述:骰子是一个立方体,每个面一个数字,初始为左1,右2,前3(观察者方向),后4,上5,下6,用123456表示这个状态,放置到平面上,可以向左翻转(用L表示向左翻转1次),可以向右翻转(用R表示向右翻转1次),可以向前翻转(用F表示向前翻转1次),可以向后翻转(用B表示向右翻转1次),可以逆时针旋转(用A表示逆时针旋转90度),可以顺时针旋转(用C表示逆时针旋转90度),现从初始状态开始,根据输入的动作序列,计算得到最终的状态。
输入描述:
初始状态为:123456
输入只包含LRFBAC的字母序列,最大长度为50,可重复
输出描述:输出最终状态
输入例子 : RA
输出例子:436512

代码:

/*
先定义几个状态:
R:左->上,上->右,右->下,下->左
L: 左->下,下->右,右->上,上->左
F:上->前,前->下,下->后,后->上
B:前->上,上->后,后->下,下->前
A:左->前,前->右,右->后,后->左
C: 前->左,左->后,后->右,右->前

再定义一个数组,保存数字,如果是R,就安上面的顺序交换数据即可

0:左,1:右,2:前,3:后,4:上,5:下
*/


#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	while (1)
	{
		string S;
		char ot[10];
		strcpy_s(ot, "123456");
		char tmp;

		getline(cin, S);
		int sz = S.size();
		for (int i = 0; i < sz; i++)
		{
			if (S[i] == 'R')
			{
				tmp = ot[0];
				ot[0] = ot[5];
				ot[5] = ot[1];
				ot[1] = ot[4];
				ot[4] = tmp;
			}
			else if (S[i] == 'L')
			{
				tmp = ot[0];
				ot[0] = ot[4];
				ot[4] = ot[1];
				ot[1] = ot[5];
				ot[5] = tmp;
			}
			else if (S[i] == 'F')
			{
				tmp = ot[4];
				ot[4] = ot[3];
				ot[3] = ot[5];
				ot[5] = ot[2];
				ot[2] = tmp;
			}
			else if (S[i] == 'B')
			{
				tmp = ot[2];
				ot[2] = ot[5];
				ot[5] = ot[3];
				ot[3] = ot[4];
				ot[4] = tmp;
			}
			else if (S[i] == 'C')
			{
				tmp = ot[2];
				ot[2] = ot[1];
				ot[1] = ot[3];
				ot[3] = ot[0];
				ot[0] = tmp;
			}
			else if (S[i] == 'A')
			{
				tmp = ot[0];
				ot[0] = ot[3];
				ot[3] = ot[1];
				ot[1] = ot[2];
				ot[2] = tmp;
			}
		}

		printf("%s\n", ot);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Aoulun/article/details/80137914