| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 759 人关注过本帖
标题:[求助]Josephus问题
只看楼主 加入收藏
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
 问题点数:0 回复次数:6 
[求助]Josephus问题

#include<iostream.h>

void main()
{
int interval,i,arraysize,q,k=1;
int *a;
loop:
cout<<"input the arraysize:";
cin>>arraysize;
cout<<"input the interval:";
cin>>interval;

if((a=new int[arraysize])==NULL)
{
cout<<"can't alloate more memory,terminating.\n";
return;
}
q=((interval>1)&&(interval<arraysize)&&(arraysize>=1));
if(q==0)
{
cout<<"输入有误!请选择 1:停止运行 2:重输 "<<endl;
if(cin.get()=='1')
return;
else
if(cin.get()=='2')
goto loop;
}
for(i=0;i<arraysize;i++)
a[i]=i+1;

for(i=0;i<arraysize;i++)
cout<<a[i]<<",";
cout<<endl;

i=-1;

while(1)
{
for(int j=0;j<interval;)
{
i=(i+1)%arraysize;
if(a[i]!=0)
j++;
}
if(k==arraysize)break;

cout<<a[i]<<",";
a[i]=0;

k++;
}

cout<<"\nNo."<<a[i]<<" boy've won.\n";

delete[]a;
}

蓝色部分判断输入是否合法,不合法的话选择1和2.在此,想问下,红色return;为何跳不出函数?
还有就是绿色部分return;,本应该为exit(1);但是编译通不过,改为return;可以通过,它们意思是一样的吧??也是跳出函数.

搜索更多相关主题的帖子: Josephus 
2007-10-16 15:50
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 
自己顶下~

★孤独的人是可耻的★
2007-10-16 17:34
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(六道)自己顶下~[em17][em17]

copied a file on my pc over.

Hope it helps.

/*---------------------------------------------------------------------------
File name: Josephus_nth_victim.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/11/2007 20:23:48
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================

Sample output

12 4 2
5 9 1 6 11 4 12 8 7 10 3 2

41 3 1
3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40
8 17 29 38 11 25 2 22 4 35 16 31

*/

#include <iostream>

using namespace std;

/** nth_victim of the Josephus circle.
* @param n --- number of people
* @param m --- step number
* @param s --- start
* @param k --- kth victim
* @return returns the original index of the kth victim.
*/
int nth_victim(int n, int m, int s, int k)
{
int p=k*m;
while(p>n)
p=(m*(p-n)-1)/(m-1);

p=(p+s-1)%n;
if(p==0)
p=n;

return p;
}


int main()
{
int n, m, s, k;
while(cin>>n>>m>>s)
{
for(k=1; k<=n; ++k)
cout<<nth_victim(n, m, s, k)<< " ";
cout<<endl<<endl;
}

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-10-16 20:35
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 
谢谢LS的,这个程序我也搞出来了,现在就是用下new和delete动态分配堆内存的方法,就是想问下,红色return;为何跳不出程序??还有绿色return那里本应该为exit(1);的(new语法下面是exit(1);),但是通不过编译.为何??

★孤独的人是可耻的★
2007-10-16 21:46
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 

楼下的朋友帮忙解释下上面的疑问,谢谢~


★孤独的人是可耻的★
2007-10-21 15:06
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
struct Boy
{
int code;
Boy *next;
};

class Ring
{
public:
Ring(int n);
void count(int m); //数m个小孩
void putboy(); //输出当前小孩编号
void clearboy();
~Ring();
protected:
Boy *pbegin; //链表的节点数组
Boy *pivot; //创建链表和当前结点
Boy *pcurrent; //创建链标建立的节点
};

Ring::Ring(int n)
{
pbegin=new Boy[n]; //分配小孩结构数组
pcurrent=pbegin;
for(int i=1;i<=n;i++,pcurrent=pcurrent->next)
{
pcurrent->code=i; //给小孩子编号
pcurrent->next=pbegin+i%n; //连接节点,构成环链
putboy();
}
cout<<endl;
pcurrent=&pbegin[n-1]; //当前小孩位置在最后一个编号
}
void Ring::count(int m)
{
for(int i=0;i<m;i++) //创建链表过程
{
pivot=pcurrent;
pcurrent=pivot->next;
}
}
void Ring::putboy()
{
static int numinline;
if(numinline++%10==0)
cout<<endl;
cout<<setw(4)<<pcurrent->code;
}
void Ring::clearboy()
{
pivot->next=pcurrent->next;
pcurrent=pivot;
}
Ring::~Ring()
{
delete[] pbegin;
}

class Jose
{
public:
Jose(int boys=10,int begin=1,int m=3) //构造函数默认初始化,在输入不合法时,按这里默认值输出
{
numofboys=boys;
beginpos=begin;
interval=m;
}
void initial();
void getwinner();
protected:
int numofboys;
int beginpos;
int interval;
};

void Jose::initial()
{
int num,begin,m;
cout<<"please input the number of boys,begin position,interval per count:\n";
cin>>num>>begin>>m;
if(num<2)
{
cerr<<"bad number of boys.\n";
return;
}
if(begin<0)
{
cerr<<"bad begin position.\n";
return;
}
if(m<1||m>num)
{
cerr<<"bad interval number.\n";
return;
}
//输入数据都合法时,予以赋值,局部变量与类数据成员同名时,覆盖了类的数据成员,不合法时,按数据成员默认值进行输出
numofboys=num;
beginpos=begin;
interval=m;
}

void Jose::getwinner()
{
Ring x(numofboys); //小孩围成圈
x.count(beginpos); //转到开始位置

for(int i=1;i<numofboys;i++) //处理除了获胜小孩之外的所有小孩
{
x.count(interval); //数小孩
x.putboy(); //输出小孩编号
x.clearboy(); //当前小孩脱离环链
}

cout<<"\nthe winner is:";
x.putboy(); //输出获胜者
}

void main()
{
Jose jose;
jose.initial();
jose.getwinner();
}


★孤独的人是可耻的★
2007-10-26 22:02
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 

不只一个人获胜的JOSEPHUS问题.函数定义放在类中
#include<iostream.h>
#include<string.h>
#include<iomanip.h>

struct Boy
{
int code;
Boy *next;
};


class Ring
{
public:
Ring(int n)
{
pbegin=new Boy[n];
pcurrent=pbegin;
for(int i=1;i<=n;i++,pcurrent=pcurrent->next)
{
pcurrent->next=pbegin+i%n;
pcurrent->code=i;
putboy();
}
cout<<endl;
pcurrent=&pbegin[n-1];
}
void count(int m)
{
for(int i=0;i<m;i++)
{
pivot=pcurrent;
pcurrent=pivot->next;
}
}
void putboy()
{
cout<<setw(4)<<pcurrent->code;
}
void display()
{
Boy *pb=pcurrent;
do
{
putboy();
pcurrent=pcurrent->next;
}while(pb!=pcurrent);

cout<<endl;
}
void clearboy()
{
pivot->next=pcurrent->next;
pcurrent=pivot;
}
~Ring()
{
delete[] pbegin;
}
protected:
Boy *pbegin; //链表的节点数组
Boy *pivot; //创建链表和当前结点
Boy *pcurrent; //创建链标建立的节点
};

class Jose
{
public:
Jose(int num=10,int begin=1,int m=3,int w=3)
{
numofboys=num;
beginpos=begin;
interval=m;
wins=w;
}
void initial()
{
int num,begin,m,w;
cout<<"please input the number of boys,begin position,interval per count,number of winners:\n";
cin>>num>>begin>>m>>w;

if(num<2)
{
cout<<"bad number of boys.\n";
return;
}
if(begin<0||begin>num)
{
cout<<"bad begin position.\n";
return;
}
if(m>num||m<1)
{
cout<<"bad interval number.\n";
return;
}
if(w<0||w>num)
{
cout<<"bad winners number.\n";
return;
}
numofboys=num;
beginpos=begin;
interval=m;
wins=w;
}

void getwinner()
{
Ring x(numofboys);
x.count(beginpos); //起始位置只定一次就OK,故在循环外

for(int i=1;i<numofboys-wins+1;i++)
{
x.count(interval); //每次都要调用-----要数几个数
x.putboy();
x.clearboy();
}

cout<<"\nthe winner is:";
x.display();
}
protected:
int numofboys;
int beginpos;
int interval;
int wins;
};

void main()
{
Jose jose;
jose.initial();
jose.getwinner();
}

[此贴子已经被作者于2007-10-26 23:42:26编辑过]


★孤独的人是可耻的★
2007-10-26 22:44
快速回复:[求助]Josephus问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018110 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved