正弦


私信TA

用户名:qq15582632151

访问量:30633

签 名:

等  级
排  名 143
经  验 7165
参赛次数 0
文章发表 57
年  龄 0
在职情况 学生
学  校 小学生
专  业

  自我简介:

解题思路:     构建循环链表,然后进行报数操作,3的倍数将序号修改为0;直到淘汰人数为总人数减一;
               
               接着遍历链表,打印序号不为0的值.
                  
注意事项:        注意循环链表,首尾相连.         别忘记点赞噢  
                       
参考代码:

#include<stdio.h>
#include<stdlib.h>

int num = 0;                                              //定义全局变量,记录淘汰人数
typedef struct node
{
	int date;
	struct node* next;                                  //定义结构体,储存序号和结构体指针
}Node;

struct list
{
	struct node *head;                                    //定义结构体,存放两个指针,方便使用,一个头指针,一个尾指针
	struct node *tail;
}List;

void init(struct list List)
{ 
	List.head = NULL;                                        //初始化结构体指针
	List.tail = NULL;
}
void creatNode(struct list *List, int data)
{
	Node *node = (Node *)malloc(sizeof(Node));
	node->date = data;
	node->next = NULL;
	if (!List->head)                                              //创建结点
	{
		List->head = List->tail = node;
	}
	else
	{
		List->tail->next = node;
		List->tail = node;
	}
}
void play(struct list *List,int n)
{
	int m = 1;
	Node *p = List->head;

	while (num < n - 1)
	{
		if (p->date)
		{                                                      //进行操作,报到3的倍数,序号修改为0
			if (m % 3 == 0)                               
			{
				p->date = 0;
				num++;
			}
			m++;
		}
		p = p->next;                                             //移动指针,指向下一个结点
	}
}
void search(struct list *List)
{
	Node *p = List->head;
	while (1)
	{                                                                  //遍历整个循环链表,出现序号不为0 ,直接打印序号
		if (p->date)
		{
			printf("%d\n", p->date);
			break;
		}
		p = p->next;
	}
}
int main()
{
	int n, i;
	scanf("%d", &n);
	init(List);
	for (i = 1; i <= n; i++)
	{
		creatNode(&List, i);
	}
	List.tail->next = List.head;                                          //尾指针指向头指针,成为循环链表
	play(&List, n);
	search(&List);
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

比我长一半
2019-05-10 16:53:33
有什么问题,  欢迎大神指教~ 也不要吝啬小心心呦
2018-07-28 13:58:21
  • «
  • 1
  • »