发布网友 发布时间:2022-05-07 01:13
共3个回答
热心网友 时间:2023-05-18 05:28
给你一个参考一下:
程序源代码:
#include<stdio.h>
typedef struct Node
{
int num;
int pasword;
struct Node *next;
}LinkList;
LinkList *creat(int n)
{
LinkList *p,*q,*head;
int i=1;
head=p=(LinkList *)malloc(sizeof(LinkList));
p->num=i;
printf("请输入第1个人的密码:");
scanf("%d",&p->pasword);
for(i=2;i<=n;i++)
{
q=(LinkList *)malloc(sizeof(LinkList));
if(q==0) return(0);
printf("请输入第%d个人的密码:",i);
scanf("%d",&q->pasword);
q->num=i;
p->next=q;
p=q;
}
p->next=head; /*使链表尾指向链表头形成循环链表*/
return head;
}
void fun(LinkList *L)
{
int m,i;
LinkList *p=L,*q,*s;
printf("请输入m的初值:");
scanf("%d",&m);
printf("出列顺序为:");
while(p->next!=p)
{
for(i=1;i<m;i++)
{ q=p;
p=p->next;
}
printf("%5d",p->num);
m=p->pasword;
s=p;
q->next=p->next;
p=p->next;
free(s);
}
printf("%5d",p->num);
printf("\n");
}
main()
{ LinkList *L;
int n;
printf("请输入实验人数:");
scanf("%d",&n);
L=creat(n);
fun(L);
}
# include <stdio.h>
# include <malloc.h>
struct stu
{
int num;
struct stu *next;
};
int m,n;struct stu *p,*q,*s;
struct stu *create()
{
struct stu *head,*s1,*r;
int i;
head=NULL;
for(i=1;i<=n;i++)
{
s1=(struct stu *)malloc(sizeof(struct stu));
s1->num =i;
if(head==NULL)head=s1;
else r->next =s1;
r=s1;
}
if(r) r->next =head;
return head;
}
struct stu * get_index(struct stu *head,int k)
{
int j=1;
if (k<1)
{
printf ("输入错误\n");
return NULL;
}
s=head;
while (s && j<k)
{
s=s->next ;j++;
}
return s;
}
void f1 (struct stu *head,struct stu *h)
{
int x,i;
x=m/2;
p=h;
i=1;printf ("依次出列的人的编号为:");
while (p->next!=p)
{
q=p->next;
for (i=1;i<x;i++)
{p=q->next;q=p->next;}
p->next =q->next ;
printf ("%d ",q->num);
free(q);
p=p->next ;
}
printf ("最后剩下的人的编号为%d\n",p->num );
}
void f2 (struct stu *head,struct stu *h)
{
int i,x;
x=m/2;
i=1;
p=h;
printf ("依次出列的人的编号为:");
do
{
for (i=1;i<=x;i++)
{q=p->next ;p=q->next ;}
q->next =p->next ;
printf ("%d ",p->num);
free(p);
p=q->next ;
}
while (q->next!=q);
printf ("\n最后剩下的人的编号为%d\n",q->num);
}
void main ()
{
int k;
struct stu *head,*h;
printf ("请输入总人数:");
scanf ("%d",&n);
printf ("请输入退出圈子的人的编号:");
scanf("%d",&m);
printf ("请输入开始报数的人的编号:");
scanf ("%d",&k);
head=create ();
h=get_index(head,k);
if (m%2==0) f1(head,h);
else f2(head,h);
}
热心网友 时间:2023-05-18 05:28
agree热心网友 时间:2023-05-18 05:29
参考代码: