BUPT数据结构第一次大作业

§ n 个加里森敢死队的队员要炸掉敌人的一个军火库,谁都不想去,队长加里森决定用轮回数数的办法来决定哪个战士去执行任务。规则如下:如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个编号为 x 的战士开始计数,当数到 y 时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第 y 时,此战士接着去执行任务。以此类推,直到任务完成为止。

加里森本人是不愿意去的,假设加里森为1号,请你设计一程序为加里森支招,求出n,x,y满足何种条件时,加里森能留到最后而不用去执行任务。

建议读者先自行思考,代码仅供参考

代码如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct node {
	int data;
	struct node* nextPtr;
}linknode;
linknode* createlist(int m)
{
	struct node* headPtr=NULL;
	struct node* currentPtr=NULL;
	struct node* lastPtr = NULL;
	int i = 1;
	while (i <= m)
	{
		currentPtr = (linknode*)malloc(sizeof(struct node));
		if (currentPtr != NULL)
		{
			currentPtr->data = i;
			if (headPtr == NULL)
			{
				headPtr = currentPtr;
				lastPtr = currentPtr;
			}
			else
			{
				lastPtr->nextPtr = currentPtr;
				lastPtr = currentPtr;
			}
		}
		i++;
	}
	lastPtr->nextPtr = headPtr;
	return headPtr;
}
int judge(struct node* headPtr,int m,int x, int y)
{
	int i = 1;
	int flag = 0;
	struct node* currentPtr = headPtr;
	struct node* previousPtr= headPtr;
	int task = 0;
	for (i = 1; i < x; i++)
	{
		if (task == 0)
		{
			currentPtr = currentPtr->nextPtr;
			task++;
		}
		else
		{
			currentPtr = currentPtr->nextPtr;
			previousPtr = previousPtr->nextPtr;
		}
	}
	int temp = y;
	while (m > 1)
	{
		temp = y;
		while (temp > 1)
		{
			if (task == 0)
			{
				currentPtr = currentPtr->nextPtr;
				task++;
			}
			else
			{
				currentPtr = currentPtr->nextPtr;
				previousPtr = previousPtr->nextPtr;
			}
			temp--;
		}
		if (currentPtr->data == 1) return 0;
		else
		{
			previousPtr->nextPtr = currentPtr->nextPtr;
			currentPtr=currentPtr->nextPtr;
			m--;
		}
	}
	if (m == 1)
	{
		return 1;
	}
}
int main()
{
	int m;
	scanf("%d", &m);
	int x = 1, y = 1;
	int flag = 0;
	for (x = 1; x <= m; x++)
	{
		flag = 0;
		for (y = 1; y <= m; y++)
		{	
			struct node* headPtr;
			headPtr = createlist(m);
			flag = judge(headPtr, m,x, y);
			if (flag == 1)
			{	printf("%d ", m);
				printf("%d %d\n", x, y);
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值