⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 13.c

📁 N个游戏者围成一圈
💻 C
字号:
//N个游戏者围成一圈,从第一个人开始顺序报数1,2,3。凡报到3者退出圈子。
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int code;
	struct node *next;
}NODE,*LinkList;
LinkList create_list(int n)
{
	LinkList head,p;
	int i;
	head=(NODE*)malloc(sizeof(NODE));
	if(!head)
	{
		printf("memory allocation error!\n");
		return NULL;
	}
	head->code=1;
	head->next=head;
	for(i=n;i>1;--i)
	{
		p=(NODE*)malloc(sizeof(NODE));
		if(!p)
		{
			printf("memory allocation error!\n"); 
			exit(1);
		}
		p->code=i; p->next=head->next; head->next=p;
	}
	return head;
}
void output(LinkList head)
{
	LinkList p;
	p=head;
	do{
		printf("%4d",p->code);
		p=p->next;
	}while(p!=head);
	printf("\n");
}
void play(LinkList head,int n)
{
	LinkList p,q;
	int c=0,k;
	p=head; c=1; k=n;
	while(k>1)
	{
		if(c==2)
		{
			q=p->next; p->next=q->next; 
			printf("%4d",q->code);  
			free(q);
			c=0; k--;
		}
		else{c++;p=p->next;}

	}
	printf("\n%4dwas the winner.",p->code);
}
void main()
{
	LinkList head;
	int n;
	printf("input the number of players:");
	scanf("%d",&n);
	head=create_list(n);
	if(head)
	{
		output(head);
		play(head,n);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -