C语言常见有关算法的笔试题之数三出局

这是博主很久之前学C语言的时候遇到的一个有关算法的问题,当时作为小白的博主做了很久才解决。
题目的大概意思是这样的:
有n个人,从1开始数数,数到三出局,问剩下那个人是谁
初见这道题的时候还是一头雾水的,但通过画图分析可以得出解决问题的思路
思路分析
下面是问题解决代码


```c
#include <stdio.h>

int main()
{
	int num;
	int other=0;  //The preservation eliminated several people.
	int i;
	int count=0; //Used to count
	int n; //Used to keep count of how many outs
	printf("Enter an integer from the keyboard to indicate how many people are out of the game!\n");
	scanf("%d",&num);
	printf("Enter an integer from the keyboard to indicate several outs!\n");
	scanf("%d",&n);
	
	//Define an array to store these n people
	int person[num];
	for(i=0; i<num; i++) //Number n people 1-n
		person[i]=i+1;
	i=0;
	while(other!=num-1)
	{
		if(person[i]!=0)  //Key point: the deleted person is set to zero
			count++;  //3
		
		if(count==n)  
		{
			person[i]=0; //On the count of three, clear
			count=0; //Restore count again
			other++; //Statistics have eliminated several people at present.
		}
		i++; //5
		if(i>num-1)  //After counting, count again.
			i=0;
	}
	//Print array,As long as it is 0--》eliminate   not 0--》The rest
	for(i=0; i<num; i++)
	{
		if(person[i]!=0)
			printf("%d个人数%d出局,剩下的是第%d个人!\n",num,n,person[i]);
	}
}

转载请声明出处:https://blog.csdn.net/xl575864781/article/details/104855180

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C语言单链表存储结构模拟约瑟夫环的示例代码,其中包含了创建链表、删除节点、输出链表等操作: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct node { int num; // 编号 int password; // 密码 struct node* next; // 指向下一个节点的指针 }; // 创建链表 struct node* create_list(int n, int* passwords) { struct node *head, *p, *q; int i; head = (struct node*)malloc(sizeof(struct node)); head->num = 1; head->password = passwords[0]; q = head; // q指向链表最后一个节点 // 创建链表 for (i = 2; i <= n; i++) { p = (struct node*)malloc(sizeof(struct node)); p->num = i; p->password = passwords[i-1]; q->next = p; q = p; } q->next = head; // 链表首尾相连,成为一个环 return head; } // 删除节点 struct node* delete_node(struct node* head, int m) { struct node *p, *q; int i; p = head; while (p->next != p) { // 当只剩一个节点时,退出循环 for (i = 1; i < m; i++) { // 找到要删除的节点的前一个节点 q = p; p = p->next; } printf("%d号出局,密码为%d\n", p->num, p->password); q->next = p->next; // 删除节点 free(p); p = q->next; // 从下一个节点重新开始报数 } printf("%d号出局,密码为%d\n", p->num, p->password); free(p); // 释放最后一个节点 return NULL; } // 输出链表 void print_list(struct node* head) { struct node* p; p = head; printf("链表存储结构示意图:\n"); do { printf("%d ", p->num); p = p->next; } while (p != head); printf("\n"); } int main() { int n = 7; // 人数 int m = 20; // 报数的数 int passwords[] = {3, 1, 7, 2, 4, 8, 4}; // 密码 struct node* head = create_list(n, passwords); print_list(head); head = delete_node(head, m); return 0; } ``` 运行结果如下: ``` 链表存储结构示意图: 1 2 3 4 5 6 7 20号出局,密码为3 2号出局,密码为1 4号出局,密码为7 1号出局,密码为2 5号出局,密码为4 3号出局,密码为8 7号出局,密码为4 ``` 以下是该约瑟夫环的存储结构示意图: ``` +---+ +---+ +---+ +---+ +---+ +---+ +---+ | 1 |--->| 2 |--->| 3 |--->| 4 |--->| 5 |--->| 6 |--->| 7 | +---+ +---+ +---+ +---+ +---+ +---+ +---+ ^ | | | | | | +-------------+ | | | | | | | | | | +---------------------------------------------+ ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值