实践案例作业

题目:

 代码如下:

#include<bits/stdc++.h>
using namespace std;
int n=13;
typedef struct student 
{
	int num;
	struct student *next;
};
int main()
{
	struct student q[20];//定义链式队列 
	struct student *head;//定义结构体型指针 
	head=q;//令指针指向队头 
	for(int i=0;i<n;i++)//依次为队列扫入 初始序号,下一位地址 
	{
		head->num=i+1;
		head->next=&q[i+1];
		head=head->next;//指针移动至下一位 
	}
	q[n-1].next=q;//令队列闭合形成环状反复筛选 
	int count=n;//记录队列总人数 
	int anum=1;//记录位置 
	head=q;//从头开始循环 
	while(count>1)
	{
		if(!head->num)//若队列数字为零 
		{
			head=head->next;
			continue;
		}
		if(anum==3)//若报数序号为三 
		{
			printf(" %d 号被淘汰\n",head->num);
			head->num=0;
			count--;
		}
		head=head->next;
		anum++;
		if(anum>3)
			anum-=3;
	}
	cout<<"---------------------\n";
	while(!head->num)
	{
		head=head->next;
		if(head->num)
		{
			printf(" %d 号留到最后\n",head->num);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值