设计一个魔方(六面)的程序 【微软面试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; }
很多时候不是我们做不好,而是没有竭尽全力......
分类:
【微软面试100题】
标签:
微软面试100题
posted on 2014-11-07 15:21 tractorman 阅读(2388) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?