利用std::set和遍历骰子的24种可能(人面向骰子)
2015-03-03 04:56
239 查看
筛子如下图,有六个
rotate.h
printdice.hh
#ifndef PRINTDICE_HH
#define PRINTDICE_HH
#include <string>
#include <set>
void printdices(std::set<string> & dices,string dice){
if(dices.size()==(unsigned)24) return;
//cout<<"size of the set:"<<dices.size()<<endl;
std::set<string>::iterator it;
bool isexist=false;
for(it=dices.begin();it!=dices.end();++it){
std::string temp=*it;
if(temp.compare(dice)==0){
isexist=true;
}
}
if(isexist==false){
cout<<dice<<endl;
dices.insert(dice);
}else{
return;
}
for(int i=1;i<=6;++i){
//cout<<"i:"<<i<<endl;
string temp=rotate(dice,i);
printdices(dices,temp);
}
}
#endif // PRINTDICE_HH
main.cc
rotate.h
#include <string> void swap(char& a, char& b, char& c, char& d){ char temp=a; a=b; b=c; c=d; d=temp; } string rotate(string dice,int rotation){ // rotation:top 1,back 2,right 3,front 4,left 5, down 6 switch(rotation){ case 1://roll front { swap(dice.at(4-1),dice.at(1-1),dice.at(2-1),dice.at(6-1)); break; } case 2://roll back { swap(dice.at(6-1),dice.at(2-1),dice.at(1-1),dice.at(4-1)); break; } case 3://roll left { swap(dice.at(1-1),dice.at(3-1),dice.at(6-1),dice.at(5-1)); break; } case 4://roll right { swap(dice.at(5-1),dice.at(6-1),dice.at(3-1),dice.at(1-1)); break; } case 5://rotate left { swap(dice.at(2-1),dice.at(3-1),dice.at(4-1),dice.at(5-1)); break; } case 6://rotate right { swap(dice.at(5-1),dice.at(4-1),dice.at(3-1),dice.at(2-1)); break; } default:break; } return dice; }
printdice.hh
#ifndef PRINTDICE_HH
#define PRINTDICE_HH
#include <string>
#include <set>
void printdices(std::set<string> & dices,string dice){
if(dices.size()==(unsigned)24) return;
//cout<<"size of the set:"<<dices.size()<<endl;
std::set<string>::iterator it;
bool isexist=false;
for(it=dices.begin();it!=dices.end();++it){
std::string temp=*it;
if(temp.compare(dice)==0){
isexist=true;
}
}
if(isexist==false){
cout<<dice<<endl;
dices.insert(dice);
}else{
return;
}
for(int i=1;i<=6;++i){
//cout<<"i:"<<i<<endl;
string temp=rotate(dice,i);
printdices(dices,temp);
}
}
#endif // PRINTDICE_HH
main.cc
#include <iostream> using namespace std; #include "complex.hh" #include "rotate.hh" #include "printdice.hh" int main() { //program2 string dice2="145326"; cout<<"start: "<<dice2<<endl; cout<<"roll front: "; cout<<rotate(dice2,1)<<endl; cout<<"turn left: "; cout<<rotate(dice2,5)<<endl; //program3 string dice3="145326"; std::set<string> set_dices; printdices(set_dices,dice3); return 0; }
相关文章推荐
- 利用Map.EntrySet遍历
- std::map遍历删除某些元素问题
- 利用索引服务(Indexing Service)和ASP建立面向整个网?
- Vista特色功能适得其反 可能被利用发动攻击
- css利用A标签的背景可能作出很有意思的效果第1/2页
- 利用前台程序控制行转列数据的输出,可能从效率和扩展性上更好.一个例子
- css利用A标签的背景可能作出很有意思的效果第1/2页
- 利用消息机制实现.NET AOP(面向方面编程)
- 自定义std::set比较函数的例子
- 本文是笔者根据数据库编程经验,利用C++语言的模板、继承、授权、多态等面向对象特性,借鉴命令模式,实现了对象在关系数据中的存储,降低应用系统与数据库之间的耦合,提高开发效率。
- 如何利用Sql 注入遍历目录
- 利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现
- 使用遍历数组、Collection、List、Set、Map等
- 利用setDaemon(true)设置守护线程
- 利用消息机制实现.NET AOP(面向方面编程)--利用RealProxy和消息接收器实现多截获
- 面向搜索引擎的内容管理系统(CMS)设计——利用Squid反相代理缓存实现网站内容静态发布
- MSN好友名单可能遭恶意利用 微软称尚不知情
- 利用消息机制实现.NET AOP(面向方面编程)--基本概念和实现
- 利用DateSet实现多个数组的快速比较(.net)
- 如何利用反射遍历某个页面下所有的控件的值!??