|
我写的小程序,题目是:不定数目的人围成一圈,随机找一个从1开始数数,报到3者退出圈子,剩下不到三人停止报数,找出退出圈子的成员.顺序,以及最后留在圈子里的人的最初的报号:
//counts.h
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct team
{
int count;
team * next;
} TEAM;
TEAM * Create_list(int count,TEAM ** globalTail)
{
TEAM * head,*tail,*p;
head = tail = NULL;
for(int i = 1; i <= count;i++)
{
p = (TEAM *)malloc(sizeof(TEAM));
p->count = i;
p->next = head;
if(head == NULL)
head = tail = p;
else
{
tail->next = p;
tail = p;
}
}
*globalTail = tail;
return head;
}
TEAM * delete_list(TEAM * head,TEAM * tail,int num )
{
TEAM * p = head;
TEAM * q;
while(p->count != num)
{
q = p;
p = p->next;
}
if(p->count == num)
{
if(p == head)
{
head = p->next;
tail = head;
}
else
q->next = p->next;
free(p);
}
return head;
}
TEAM * note_list(TEAM * head,TEAM * tail,int count)
{
TEAM * p = head,*q = NULL;
int note = 1;
int num = 0;
int rest = 0;
while(count >= 3)
{
if((note)%3 != 0)
p = p->next;
else
{
q = p->next;
num = p->count;
printf("\nThe out num is %d",num);
delete_list(head,tail,num);
p = q;
--count;
}
++note;
}
for(int i = 1;i <=count;i++)
{
printf("\nThe left is %d",p->count);
p = p->next;
}
return head;
}
#include "Counts.h"
void main()
{
TEAM *tail;
int n;
printf("Please input the Number:\n");
scanf("%5d",&n);
TEAM * head = Create_list(n,&tail);
note_list(head,tail,n);
}
有的值能成功运行,有的却不行,请高手指点,谢谢
[ 本帖最后由 dusx1981 于 2007-8-24 14:09 编辑 ] |
|