主题:自己写的作业题,不知道哪里错了……请帮忙看看
一约瑟夫环问题
#include<stdio.h>
#include<malloc.h>
typedef struct node
{int num;
struct node *next;
}linklist;
linklist *creat(head,n)/*使n个人围成一圈,并给每个人标识号数*/
linklist *head;
int n;
{linklist *s,*p;
int i;
s=malloc(sizeof(linklist));
head=s;
s->num=1;
p=s;
for(i=2;i<=n;i++)
{s=malloc(sizeof(linklist));
s->num=i;
p->next=s;
p=s;
}
p->next=head;
return head;
}
/*creat*/
linklist *select(head,m)
linklist *head;
int m;
{linklist *p,*q;
int i,t;
p=head;
t=1;
q=p;/*q为p的前趋指针*/
do
{p=q->next;/*p指向当前报数的人*/
t=t+1;
if(t%m==0)
{printf("%4d",p->num);
q->next=p->next;
free(p);
}
else
q=p;
}
while(q==p)
head=p;
return(head);
}
/*select*/
main()
{int n,m;
linklist *head;
printf("input the total number\n");
scanf("%d",&n);
printf("input the number to call:\n");
scanf("%d",&m);
creat(head,n);
select(head,m);
printf("the last one:is\n",head->num);
}
/*main*/
谢谢!
#include<stdio.h>
#include<malloc.h>
typedef struct node
{int num;
struct node *next;
}linklist;
linklist *creat(head,n)/*使n个人围成一圈,并给每个人标识号数*/
linklist *head;
int n;
{linklist *s,*p;
int i;
s=malloc(sizeof(linklist));
head=s;
s->num=1;
p=s;
for(i=2;i<=n;i++)
{s=malloc(sizeof(linklist));
s->num=i;
p->next=s;
p=s;
}
p->next=head;
return head;
}
/*creat*/
linklist *select(head,m)
linklist *head;
int m;
{linklist *p,*q;
int i,t;
p=head;
t=1;
q=p;/*q为p的前趋指针*/
do
{p=q->next;/*p指向当前报数的人*/
t=t+1;
if(t%m==0)
{printf("%4d",p->num);
q->next=p->next;
free(p);
}
else
q=p;
}
while(q==p)
head=p;
return(head);
}
/*select*/
main()
{int n,m;
linklist *head;
printf("input the total number\n");
scanf("%d",&n);
printf("input the number to call:\n");
scanf("%d",&m);
creat(head,n);
select(head,m);
printf("the last one:is\n",head->num);
}
/*main*/
谢谢!