zoukankan      html  css  js  c++  java
  • 设计一个魔方(六面)的程序 【微软面试100题 第四十四题】

    题目要求:

      设计一个魔方(六面)的程序。

    题目分析:

      把魔方从正面看展开成一个平面,如图1所示。设计一个类,其中Spacexy[SPACE][LEN][LEN];中的SPACE为0~5表示六个面,每个数字代表哪一面见图1.LEN为0~2,[LEN][LEN]表示某个面的3*3的9个格子。

      类中的方法是根据展开的平面设计的,具体的某个面的某个格子由Spacexy[SPACE][LEN][LEN];定位。

    代码实现:

    #include <iostream>
    using namespace std;
    
    class MagicCube
    {
    private:
        enum{LEN = 3,SPACE = 6};
        enum color{red,yellow,black,blue,green,purple};
        enum color Spacexy[SPACE][LEN][LEN];
    public:
        MagicCube();
        ~MagicCube(){};
        void LeftRotate(int x,int y);
        ////void RightRotate(int x,int y);
        void UpRotate(int x,int y);
        //void DownRotate(int x,int y);
        void PrintCube();
    };
    void MagicCube::UpRotate(int x,int y)
    {
        color tmp[3];
    
        for(int i = 0;i<3;i++)
            tmp[i] = Spacexy[0][i][y];
    
        for(int i = 0;i<3;i++)
            Spacexy[0][i][y] = Spacexy[5][i][y];
        for(int i = 0;i<3;i++)
            Spacexy[5][i][y] = Spacexy[2][i][2-y];
        for(int i = 0;i<3;i++)
            Spacexy[2][i][2-y] = Spacexy[4][i][y];
        for(int i = 0;i<3;i++)
            Spacexy[4][i][y] = tmp[i];
    }
    void MagicCube::PrintCube()
    {
        for(int s = 0;s<6;s++)
        {
            switch(s)
            {
            case 0:cout << " 正面:" << endl; break;
            case 1:cout << " 右面:" << endl; break;
            case 2:cout << " 后面:" << endl; break;
            case 3:cout << " 左面:" << endl; break;
            case 4:cout << " 上面:" << endl; break;
            case 5:cout << " 下面:" << endl; break;
            default:break ;
            }
            for(int i = 0;i<3;i++)
            {
                for(int j = 0;j<3;j++)
                {
                    cout << Spacexy[s][i][j] << " ";
                }
                cout << endl;
            }
            cout << endl;
        }
    
        cout << "---------------------------------------" << endl;
    }
    MagicCube::MagicCube()
    {
        for(int i = 0;i<6;i++)//每一面一个颜色
            for(int j = 0;j<3;j++)
                for(int k = 0;k<3;k++)
                {
                    Spacexy[i][j][k] = (color)i;
                }
    }
    void MagicCube::LeftRotate(int x,int y)
    {
        color tmp[3];
    
        for(int i = 0;i<3;i++)
            tmp[i] = Spacexy[0][x][i];
    
        for(int i = 0;i<3;i++)
            Spacexy[0][x][i] = Spacexy[1][x][i];
        for(int i = 0;i<3;i++)
            Spacexy[1][x][i] = Spacexy[2][x][i];
        for(int i = 0;i<3;i++)
            Spacexy[2][x][i] = Spacexy[3][x][i];
        for(int i = 0;i<3;i++)
            Spacexy[3][x][i] = tmp[i];
    
    }
    int main(void )
    {
        MagicCube a;
        a.PrintCube();
        a.UpRotate(0,0);
        a.PrintCube();
        return 0;
    }
  • 相关阅读:
    DELPHI版传奇引擎学习菜鸟篇(applem2)04
    DELPHI版传奇引擎学习菜鸟篇(applem2)03
    学习win32API消息处理
    DELPHI版传奇引擎学习菜鸟篇(applem2)02
    DELPHI版传奇引擎学习菜鸟篇(applem2)01
    读写INI的通用函数
    黑马程序员选择语句 SwitchCase 和break 、return 关键字。
    黑马程序员Readonly和Const的区别
    黑马程序员计算每个字符在字符串中出现的次数
    黑马程序员ADO.NET中的五个主要对象
  • 原文地址:https://www.cnblogs.com/tractorman/p/4081422.html
Copyright © 2011-2022 走看看