编写程序实现约瑟夫(Josephu)问题(C)
来源:互联网 发布:幼儿园大班弄的美工 编辑:程序博客网 时间:2024/04/29 02:14
#include <stdio.h>
struct node{
int id;
int password;
struct node* next;
}l;
del(int m)
{
struct node* p;
struct node* next;
int j;
next=&l;
for(j=1;j<=5;j++)
{
/*printf("id为%d的下个结点为%d ",next->id,next->next->id);*/
next=next->next;
}
next=&l;
while (next->id!=next->next->id)
{
for(j=1;j<=m;j++) next=next->next;
p=next->next;
printf("删除第[%d]个结点 ",p->id );
next->next=next->next->next;
m=next->password;
}
printf("complete");
getch();
}
initl(int n,int m,int k)
{
struct node* p,*next;
int j;
l.password=k;
l.id=1;
l.next=&l;
next=&l;
printf("建立链表结点[1] password=%d ",m);
for (j=2;j<=n;j++)
{
p=(struct node*)malloc(sizeof(struct node));
next->next=p;
next=p;
p->id=j;
p->password =rand();
p->next=&l;
printf("建立链表结点[%d] password=%d ",p->id,p->password);
}
del(m);
}
main(){
/*m为祲密码n为结点个数k*/
int n,m,k;
printf("请输入结点个数n: ");
scanf("%d",&n);
printf("请输入初始密码k: ");
scanf("%d",&k);
printf("请输入删除结点时的随机数m: ");
scanf("%d",&m);
initl(n,m,k);
}
struct node{
int id;
int password;
struct node* next;
}l;
del(int m)
{
struct node* p;
struct node* next;
int j;
next=&l;
for(j=1;j<=5;j++)
{
/*printf("id为%d的下个结点为%d ",next->id,next->next->id);*/
next=next->next;
}
next=&l;
while (next->id!=next->next->id)
{
for(j=1;j<=m;j++) next=next->next;
p=next->next;
printf("删除第[%d]个结点 ",p->id );
next->next=next->next->next;
m=next->password;
}
printf("complete");
getch();
}
initl(int n,int m,int k)
{
struct node* p,*next;
int j;
l.password=k;
l.id=1;
l.next=&l;
next=&l;
printf("建立链表结点[1] password=%d ",m);
for (j=2;j<=n;j++)
{
p=(struct node*)malloc(sizeof(struct node));
next->next=p;
next=p;
p->id=j;
p->password =rand();
p->next=&l;
printf("建立链表结点[%d] password=%d ",p->id,p->password);
}
del(m);
}
main(){
/*m为祲密码n为结点个数k*/
int n,m,k;
printf("请输入结点个数n: ");
scanf("%d",&n);
printf("请输入初始密码k: ");
scanf("%d",&k);
printf("请输入删除结点时的随机数m: ");
scanf("%d",&m);
initl(n,m,k);
}
- 编写程序实现约瑟夫(Josephu)问题(C)
- 约瑟夫问题(Josephu)
- Josephu(约瑟夫)问题(java版本)
- [JAVA练习] Josephu 约瑟夫问题
- erl_线性结构-Josephu(约瑟夫问题 )问题
- Josephu问题-下——实现程序功能
- Josephu 问题的实现方法
- 约瑟夫问题程序实现
- 用链表实现约瑟夫问题(c/C++)
- Josephu程序
- 用循环链表解决约瑟夫(josephu)问题
- 约瑟夫josephu算法
- 简单约瑟夫环(josephu)
- 约瑟夫环Josephu
- JosePhu问题
- Josephu问题
- 约瑟夫问题(c++)
- Josephu 问题 (add pin number)
- eclipsework学习笔记
- JSP错误:JSPG0227E: JSPG0123E
- 给c++学习者的书目
- 想把家搬过来了
- epoll使用的一个小例子,非常实用
- 编写程序实现约瑟夫(Josephu)问题(C)
- 晒晒自己编写的软件
- ORA-04021错误数据库表锁定错误
- 矩阵转置(C)
- BDS 2006 Hotfix Rollup 2
- WinMount v1.0
- 字符集的修改
- 回头望月
- 编写程序实现n阶(n为奇数)魔方(C)