瑟夫环问题
题目是50是个人站在一圈,依次编号1,2,3,......50.第一个人开始数数1,2,3 到3的人推出来 第四个人接着数1,2,3 这样下去 。直到剩下一个人,问剩下的这个人是原来的多少号?我的程序是下面:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{int num;
struct student *next;
};
struct student *creat(void)
{struct student *head ,*p1,*p2;
int n,i;
p1=p2=(struct student *)malloc(LEN);
i=1;n=0;
p1->num=i;
head=NULL;
while(p1->num<=50)
{n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
p1->num=++i;
}
p2->next=head;
return(head);
}
struct student *del(struct student *head,int num)
{struct student *p1,*p2;int i,n;
do
{if(n==50)p1=head;
else p1=p1->next;
for(i=0;i<=2;i++)
{p2=p1;
p1=p1->next;
}
p2->next=p1->next;
n--;
}
while(n>1);
return(head);
}
void main()
{struct student *p;
p=del(creat(),50);
if(p!=NULL)
do
{printf("%d",p->num);
p=p->next;
}
while(p!=NULL);
}
不知道是哪里有问题,就是运行不出来结果
有哪位高手,请指点。
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{int num;
struct student *next;
};
struct student *creat(void)
{struct student *head ,*p1,*p2;
int n,i;
p1=p2=(struct student *)malloc(LEN);
i=1;n=0;
p1->num=i;
head=NULL;
while(p1->num<=50)
{n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
p1->num=++i;
}
p2->next=head;
return(head);
}
struct student *del(struct student *head,int num)
{struct student *p1,*p2;int i,n;
do
{if(n==50)p1=head;
else p1=p1->next;
for(i=0;i<=2;i++)
{p2=p1;
p1=p1->next;
}
p2->next=p1->next;
n--;
}
while(n>1);
return(head);
}
void main()
{struct student *p;
p=del(creat(),50);
if(p!=NULL)
do
{printf("%d",p->num);
p=p->next;
}
while(p!=NULL);
}
不知道是哪里有问题,就是运行不出来结果
有哪位高手,请指点。
...全文
相关推荐
瑟夫环问题(rotate解法)
随便写写的......
约瑟夫(Josephus)环问题
数据结构课程设计 约瑟夫(Josephus)环问题:编号为1,2,3,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数的上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止。报m的人出列,将他的密码作为新的m值,从他在顺时针方向上的下一人开始重新从1报数,如此下去,直到所有人全部出列为止。 建立n个人的单循环链表存储结构,运行结束后,输出依次出队的人的序号。
多瑟夫环问题单向循环链表
约瑟夫问题的一种描述是:编号为1,2,...,n的n个人按顺时针方向围坐 一圈,每人持有一个密码。
约瑟夫问题--环问题
约瑟夫环问题,多的不说了,内行都明白的。 就是一群人,从中选择一个人 这是自己写的,有比较详细的注释,大家自己读哈,谢谢诶
约瑟夫(Josephus)环问题:编号为1,2,3,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)
约瑟夫环2、 约瑟夫(Josephus)环问题:编号为1,2,3,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数的上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止。报m的人出列,将他的密码作为新的m值,从他在顺时针方向上的下一人开始重新从1报数,如此下去,直到所有人全部出列为止。 建立n个人的单循环链表存储结构,运行结束后,输出依次出队的人的序号。


新手乐园
