帮忙看看哪里错了。。。
题目:n个人围成一个圈,按顺序报数1,2,3,凡是报3者推出圈子,找出最后留在圈子的人的原来的序号,用链表处理。
程序代码:#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct People)
struct People
{
int num;
int count;
struct People *next;
};
struct People *creat(int n)
{
int i=1;
struct People *p1,*p2,*head;
head=p1=p2=(struct People *)malloc(LEN);
while(n)
{
p1->num=i;
p1->count=0;
p1=(struct People *)malloc(LEN);
p2->next=p1;
p2=p1;
i++;
n--;
}
p2->next=head;
return head;
}
void del(struct People *pt)
{
}
void main()
{
int n;
struct People *head,*p1,*p2;
printf("人数n=");
scanf("%d",&n);
head=creat(n);
p1=p2=head;
while(n!=1)
{
p1->count=p2->count+1;
if(p1->count==3)
{
p2->next=p1->next;
n--;
p1=p1->next;
p2=p1;
p1->count=1;
p1=p1->next;
}
else
{
p2=p1;
p1=p1->next;
}
}
printf("最后留下的人原来号码为:%d\n",p1->num);
}例如,如果是5个人围成一圈的话,最后留下来的应该是4号。。
[ 本帖最后由 wwfdzh2012 于 2013-5-6 22:28 编辑 ]









