比较容易的一题,但是要心细(写了好久)
按照题目的要求展开二阶魔方:
输入x时,顺时针移动带框的部分
输入y和z同样移动相应部分。
有一点需注意:
e.g.当输入x时,绿色这一面需要顺时针翻转一下:
0 1 👉 2 0
2 3 👉 3 1
因为一面都是同一种颜色所以很容易被忽视
#include<iostream>
using namespace std;
int Front[4] = { 1,1,1,1 }; //green
int Back[4] = { 2,2,2,2 }; //blue
int Up[4] = { 3,3,3,3 }; //white
int Down[4] = { 4,4,4,4 }; //yellow
int Left[4] = { 5,5,5,5 };//orange
int Right[4] = { 6,6,6,6 }; //red
void Turn_x();
void Turn_y();
void Turn_z();
void output();
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == 'x') Turn_x();
else if (str[i] == 'y') Turn_y();
else if (str[i] == 'z') Turn_z();
}
output();
return 0;
}
void output()
{
int A[] = { Front[1],Right[0],Up[3] };
for (int i = 0; i < 3; i++)
{
//string color="绿蓝白黄橙红";
switch (A[i])
{
case 1:cout << "绿"; break;
case 2:cout << "蓝"; break;
case 3:cout << "白"; break;
case 4:cout << "黄"; break;
case 5:cout << "橙"; break;
case 6:cout << "红"; break;
}
}
}
void Turn_x()
{
int temp1, temp2, temp3, temp4, temp5, temp6;
temp1 = Up[2], temp2 = Up[3];
temp3 = Right[0], temp4 = Right[2];
temp5 = Down[0], temp6 = Down[1];
Up[2] = Left[1], Up[3] = Left[3];
Right[0] = temp1, Right[2] = temp2;
Down[0] = temp3, Down[1] = temp4;
Left[1] = temp5, Left[3] = temp6;
temp1 = Front[0], temp2 = Front[1], temp3 = Front[2], temp4 = Front[3];
Front[0] = temp3, Front[1] = temp1, Front[2] = temp4, Front[3] = temp2;
}
void Turn_y()
{
int temp1, temp2, temp3, temp4, temp5, temp6;
temp1 = Up[1], temp2 = Up[3];
temp3 = Back[1], temp4 = Back[3];
temp5 = Down[1], temp6 = Down[3];
Up[1] = Front[1], Up[3] = Front[3];
Back[1] = temp1, Back[3] = temp2;
Down[1] = temp3, Down[3] = temp4;
Front[1] = temp5, Front[3] = temp6;
temp1 = Right[0], temp2 = Right[1], temp3 = Right[2], temp4 = Right[3];
Right[0] = temp3, Right[1] = temp1, Right[2] = temp4, Right[3] = temp2;
}
void Turn_z()
{
int temp1, temp2, temp3, temp4, temp5, temp6;
temp1 = Left[0], temp2 = Left[1];
temp3 = Back[2], temp4 = Back[3];
temp5 = Right[0], temp6 = Right[1];
Left[0] = Front[0], Left[1] = Front[1];
Back[2] = temp1, Back[3] = temp2;
Right[0] = temp3, Right[1] = temp4;
Front[0] = temp5, Front[1] = temp6;
temp1 = Up[0], temp2 = Up[1], temp3 = Up[2], temp4 = Up[3];
Up[0] = temp3, Up[1] = temp1, Up[2] = temp4, Up[3] = temp2;
}