用C++实现约瑟夫环的问题
2014-10-29 21:54
232 查看
4000
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。
假定在圈子里前K个为好人,后K个为坏人,你的任务是确定这样的最少M,使得所有的坏人在第一个好人之前被杀掉。
//----数学中有乘法口诀。。那只是工具。我们都很熟悉。
//----C++中有一些基本的程序。也只是工具。我们必须像熟悉乘法口诀一样去熟悉这些程序。
//----很基础的一些东西,必须熟练。。。
#include<iostream>
class link;
using namespace std;
class node{
friend class link;
public:
node():next(NULL){}
node(int value):data(value),next(NULL){}
private:
int data;
node *next;
};
class link{
public:
link(int x,int y,int z):n(x),s(y),m(z){}
node *createlink()
{
node *p,*r;
node *q;
r=p=new node;
for(int i=1;i<=n;++i)
{
q=new node;
q->data=i;
r->next=q;
r=q;
}
r->next=p->next;
return p;
}
node *jusefu(node *startnode)
{
node *p=startnode->next;
node *q;
for(int i=1;i<s;++i)
p=p->next;//让p指向开始数数的位置,让q指向需要删除结点的位置的前一个位置
q=p->next;
while(q->next!=p)
{
q=q->next;
}
while(p->next!=p)
{
for(int j=1;j<m;++j)
{
//node *tmp;
q=p;
p=p->next;
}
q->next=p->next;
cout<<"将要删除的号码是"<<p->data<<endl;
delete p;
p=q->next;
}
cout<<"留下来的人数的号码为"<<p->data<<endl;
return p;
}
private:
int n;
int s;
int m;
};
int main()
{ int i,j,k;
cout<<"输入总数,开始位置;每次循环人数"<<endl;
cin>>i>>j>>k;
link linklist(i,j,k);
node *head=linklist.createlink();
node *lastnode=linklist.jusefu(head);
system("pause");
return 0;
}
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。
假定在圈子里前K个为好人,后K个为坏人,你的任务是确定这样的最少M,使得所有的坏人在第一个好人之前被杀掉。
//----数学中有乘法口诀。。那只是工具。我们都很熟悉。
//----C++中有一些基本的程序。也只是工具。我们必须像熟悉乘法口诀一样去熟悉这些程序。
//----很基础的一些东西,必须熟练。。。
#include<iostream>
class link;
using namespace std;
class node{
friend class link;
public:
node():next(NULL){}
node(int value):data(value),next(NULL){}
private:
int data;
node *next;
};
class link{
public:
link(int x,int y,int z):n(x),s(y),m(z){}
node *createlink()
{
node *p,*r;
node *q;
r=p=new node;
for(int i=1;i<=n;++i)
{
q=new node;
q->data=i;
r->next=q;
r=q;
}
r->next=p->next;
return p;
}
node *jusefu(node *startnode)
{
node *p=startnode->next;
node *q;
for(int i=1;i<s;++i)
p=p->next;//让p指向开始数数的位置,让q指向需要删除结点的位置的前一个位置
q=p->next;
while(q->next!=p)
{
q=q->next;
}
while(p->next!=p)
{
for(int j=1;j<m;++j)
{
//node *tmp;
q=p;
p=p->next;
}
q->next=p->next;
cout<<"将要删除的号码是"<<p->data<<endl;
delete p;
p=q->next;
}
cout<<"留下来的人数的号码为"<<p->data<<endl;
return p;
}
private:
int n;
int s;
int m;
};
int main()
{ int i,j,k;
cout<<"输入总数,开始位置;每次循环人数"<<endl;
cin>>i>>j>>k;
link linklist(i,j,k);
node *head=linklist.createlink();
node *lastnode=linklist.jusefu(head);
system("pause");
return 0;
}
相关文章推荐
- [面试] 面向对象的约瑟夫环问题的通法 [ C++实现 ] [为夏逸轩师弟作]
- 约瑟夫环问题及python与c++实现效率对比
- 约瑟夫环的问题编程C++实现
- [面试] 面向对象的约瑟夫环问题的通法 [ C++实现 ] [为夏逸轩师弟作]
- 用C++实现约瑟夫环的问题
- 约瑟夫环问题(顺序表)——C++实现
- 【c++】约瑟夫环问题的链表实现
- 约瑟夫环问题的实现
- 从易到难编写C++程序,(8)问题:实现一个矩阵类
- C/C++面试之算法系列--约瑟夫环:每隔两个循环删除数组元素,求最后删除者的下标问题
- 一笔画问题的c++实现
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(3) 最大子序列和问题
- 个人研究《数据结构与算法分析-C++描述》Vector实现的问题,new与初始化
- 从易到难编写C++程序,(3)问题:实现一个复杂的猜数字游戏
- 【转】用c++实现的8皇后问题
- 单链表实现猴子选大王(约瑟夫环问题)
- 八皇后问题的c++实现
- 从易到难编写C++程序,(7)问题:利用问题(6)的发牌,实现24点游戏
- 《数据结构与算法分析-C++描述》List实现的问题,g++太符合标准,以至于有的时候虽然正确,但是却会让你吃惊
- 微软过桥问题的C++实现