当前位置: 代码迷 >> C语言 >> 约瑟夫问题
  详细解决方案

约瑟夫问题

热度:387   发布时间:2005-12-08 20:02:00.0
约瑟夫问题
1,用环链??

2.用数组??

谁做下!
搜索更多相关的解决方案: 约瑟夫  

----------------解决方案--------------------------------------------------------
环链
struct circle{
int no;序号
struct circle *next;
}x[30],*head,*p,*p1;
main()
{head=x;
p=head;
int count;计数器;
建环链,
for(i=1;i<=29;i++)
{p->no=i;
p->next=&x[i];
p=p->next;
}
p->no=30;
p->next=head;
for(i=2;count<15;i++)
{p1=p;
p=p->next;
if((i+1)%9==0)
{p1->next=p->next;断链
p=p1->next;
i=1;重置
count=++count;
}
}
for(i=0;i<=14;i++)
{printf("%d",p->no);打印出还在船上的序号>
p=p->next;
}

[此贴子已经被作者于2005-12-9 18:00:48编辑过]


----------------解决方案--------------------------------------------------------
http://alt26.nease.net/Computer/mypro_01.htm
----------------解决方案--------------------------------------------------------
不就是猴子选大王的问题拉
前两天我才写了,顺便给你复制过来吧
/**********************************************/
#include<iostream>
using namespace std;

long int n,m;

struct Monkey
{
long int number;
struct Monkey *next;
};
struct Monkey *end;
struct Monkey *creat()
{
struct Monkey *q,*p,*head;
long int i=1;

while (i<=m)
{
if(i!=1) q=p;
p=new(struct Monkey);
if (i==1) head=p;
p->number=i;
if (i!=1) q->next=p;
i++;
}
p->next=head;end=p;
return(head);
}

void del(struct Monkey *q,struct Monkey *p)
{
q->next=p->next;
delete(p);
}

long int chooseking()
{
struct Monkey *p,*q;long int i=1;
p=creat();q=end;

while (p->next!=p)
{
lp:if(i==n) {del(q,p);p=q->next;i=1;if (p->next==p) break;goto lp;}
q=p;
p=p->next; i=i+1;
}
return(p->number);
}

void main()
{
cout<<"please m and n:";
cin>>m>>n;
cout<<chooseking();
}


----------------解决方案--------------------------------------------------------

楼上的不错,这题用静态链就可以了,

goto 都用到了,强

[此贴子已经被作者于2005-12-9 18:45:19编辑过]


----------------解决方案--------------------------------------------------------
楼上的说的反语吧?
----------------解决方案--------------------------------------------------------
  相关解决方案