| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 662 人关注过本帖
标题:[请教]猴子选大王
收藏  订阅  推荐  打印 
herry
Rank: 2
等级:注册会员
帖子:43
积分:530
注册:2007-5-26
[请教]猴子选大王

猴子选大王
  一群猴子都有编号,编号是1,2,3 ...m ,这群猴子(m个)按照1——m的顺序围坐一圈,从第1开始数,每数到第N个,该猴子就要离开此圈,然后继续从1开始,这样依次下来,直到圈中只剩下一只猴子,则该猴子为大王。
搜索更多相关主题的帖子: 大王  猴子  
2007-6-23 15:13
herbert_1987
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:15
帖子:1313
积分:13230
注册:2007-5-13

又是约瑟夫环问题,用循环链表,然后逐个删除.

人生重要的不是所站的位置,而是所朝的方向
2007-6-23 15:18
冰天雪
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:331
积分:3426
注册:2007-2-27

算法还没弄懂
2007-6-23 15:34
herry
Rank: 2
等级:注册会员
帖子:43
积分:530
注册:2007-5-26

约瑟夫环是什么啊?好像还没有听说过哦?
2007-6-23 15:46
herry
Rank: 2
等级:注册会员
帖子:43
积分:530
注册:2007-5-26

去搜了一下,终于知道怎么回事了,谢谢了!!!
2007-6-23 15:52
herbert_1987
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:15
帖子:1313
积分:13230
注册:2007-5-13

我这里有一个程序(不过不是很完善D):
///////////////////////////头文件////////////////
typedef struct member
{
int ID;//某个人的序号
int cipher;//密码
member *next;//下一个

}Person;

typedef struct
{
Person *head;//链表头
Person *tail;//链表尾
int lenth;//链表长
}List;

///////////// 算法部分 ( .cpp 文件)////////////
//添加链表元素
void AddMember(List &list,int ID,int cipher)
{
Person *per = (Person *)malloc(sizeof(Person));
per->ID = ID;
per->cipher = cipher;

//当链表为空时
if(list.lenth == 0)
{
list.head = per;
list.tail = per;
per->next = list.head;
}
else //当链表不空时
{
per->next = list.head;
list.tail->next = per;
list.tail = per;
}
list.lenth++;
}

//用来记录输出结果的数组 初始化
void InitArray(int lenth, int **array)
{
*array = (int *)malloc(lenth * sizeof(int));
}


//处理链表(排序)
void DoList(List list, int *array)
{
int N = list.lenth;//用于循环
int arrayCount = 0;//输出数组的下标
int cip;
Person *person = list.head;
cip = person->cipher;
while(N > 0)
{
for(int i = 1; i < cip; i++)//找出下一个出列的人
person = person->next;

Person *p = person->next;

cip = p->cipher;
array[arrayCount] = p->ID;
arrayCount++;

person->next = p->next;
free(p);
N--;
}


}


//////////////main 函数部分(.cpp 文件)/////////////
#include <iostream.h>
#include <malloc.h>
#include <stdlib.h>

#include "test2.h"
#include "algo2.cpp"


void main()
{
int max, n;
int cipher;

List list;
char loop;//循环的标志

do
{
cout<<"*************** 约瑟夫环问题 ****************"<<endl<<endl;

do
{
cout<<"输入密码的最大值: ";
cin>>max;
cout<<endl<<"输入人数: ";
cin>>n;

if(max <= 0 || n <= 0)
cout<<"你所输入的数不能小于 1,请重新输入"<<endl;
}
while(max <= 0 || n <= 0);
list.lenth = 0;

for(int i = 1; i <= n; i++)
{
cout<<"输入第 "<<i<<" 个人的密码(必须大于0): ";
do
{
cin>>cipher;
if(cipher > max)
cout<<"你所输入的数大于密码的最大值,请从新输入:"<<endl;
if(cipher <= 0)
cout<<"你所输入的数小于 1,请从新输入:"<<endl;
}
while(cipher > max || cipher <= 0);

AddMember(list, i, cipher);//添加成员
}

int *array;
InitArray(n, &array);//数组初始化

DoList(list, array);//按顺序删除成员

cout<<endl<<"************* 出列顺序为 ***************"<<endl;
for(i = 1; i <= n; i++)//输出出列顺序
{
if(i%20 == 0)
cout<<endl;
cout<<array[i-1]<<" ";
}

cout<<endl<<"是否要继续?(Y / N)"<<endl;
cin>>loop;
system("cls");//清屏幕
}
while(loop == 'Y' || loop == 'y');
}


人生重要的不是所站的位置,而是所朝的方向
2007-6-23 15:53
zkkpkk
Rank: 3Rank: 3
等级:中级会员
威望:4
帖子:474
积分:4866
注册:2006-6-17

约瑟夫环,本来是说将异教徒丢下海的,后来变种为猫吃老鼠,小孩报数,现在连猴子也出来了

我的阿根廷队何时涅磐啊,唉......
2007-6-23 21:40
herbert_1987
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:15
帖子:1313
积分:13230
注册:2007-5-13

以下是引用zkkpkk在2007-6-23 21:40:09的发言:
约瑟夫环,本来是说将异教徒丢下海的,后来变种为猫吃老鼠,小孩报数,现在连猴子也出来了

呵呵, 挺有趣的!^_^


人生重要的不是所站的位置,而是所朝的方向
2007-6-23 21:48
滨海
Rank: 1
等级:新手上路
帖子:38
积分:480
注册:2007-6-3

约瑟夫环问题,

你看看吧

#include <iostream>
using namespace std;
void main()
{
int A[9],s,m; //相关参数的定义和输入;
cin>>s>>m;
int f,k,t,count=0;
if(m==0)
{
cerr<<"输入的参数无效!"<<endl;
return;
}
for( int i=0;i<9;i++)A[i]=i+1; //初始化数组
f=s-1; //起始报数者;
for(k=9;k>=1;k--) //循环出局;
{
if(f==k-1)f=0;
f=(f+m-1)%k;
if(f!=k-1)
{
t=A[f];
for(int j=f;j<k-1;j++)A[j]=A[j+1]; //将淘汰者依次往后移;
A[k-1]=t;
}
count++; //统计出局的次序;
cout<<"第"<<count<<"个出局:"<<A[k-1]<<endl;
}
cout<<"游戏结束!"<<endl;
}

让暴风雨来的更猛烈些吧!!
2007-6-25 23:13
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.061935 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved