#include"stdio.h"
#include"conio.h"
#include"malloc.h"
#define LEN sizeof(Node)
#define true 1
#define false 0
typedef struct tagNode
{
int Number;
struct tagNode *next;
}Node;
int n;
void DeleteNode(Node *pre,Node *p)
{
pre->next=p->next;
free(p);
}
int IsFinished(Node *p)
{
if(p->next==p)
return true;
else
return false;
}
Node *CreateList(void)
{
Node *head;
Node *p1,*p2;
n=0;
printf("构建链表以0结束符号:\n");
p1=p2=(struct tagNode *)malloc(LEN); //开辟一个新单元
scanf("%d",&p1->Number);
head=NULL;
while(p1->Number!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct tagNode *)malloc(LEN);
scanf("%d",&p1->Number);
}
p2->next=head;
return head;
}
void Josehus(Node *head,int Counter)
{
Node *pre,*p;
int Number=1;
p=pre=head;
while(!IsFinished(pre))
{
while(Number!=Counter)
{
p=pre;
pre=pre->next;
Number++;
}
printf("%d ",pre->Number);
DeleteNode(p,pre);
pre=p->next;
Number=1;
}
printf("%d\n",pre->Number);
}
int main()
{
int Counter;
Node *head;
head=CreateList();
printf("Input Your Counter:\n");
scanf("%d",&Counter);
while((Counter<1)||(Counter>5))
{
printf("Input Error!Please Input 1<=Counter <=5:\n");
scanf("%d",&Counter);
}
printf("输出出列的编号:\n");
Josehus(head,Counter);
}
没有合适的资源?快使用搜索试试~ 我知道了~
约瑟夫问题 设有N个人围坐一圈,现从某人开始报数, 数到M的人出列,接着从出列的下一个人重新报数,数到M的人...
共3个文件
h:2个
c:1个
3星 · 超过75%的资源 需积分: 45 76 下载量 10 浏览量
2009-04-11
10:42:10
上传
评论 8
收藏 2KB RAR 举报
温馨提示
Josehus问题算法 设有N个人围坐一圈,现从某人开始报数, 数到M的人出列,接着从出列的下一个人重新报数,数到M的人又出列,如此下去直到所有人都出列为止,给出他们的出列次序。
资源推荐
资源详情
资源评论
收起资源包目录
.rar (3个子文件)
约瑟夫问题
stdafx.h 233B
targetver.h 498B
Josehus.c 2KB
共 3 条
- 1
资源评论
- h2689652020-06-22竟然还要自己构造链表,输入0表示输出结束
xu3029365
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功