当前位置: 首页 > news >正文

约瑟夫生死游戏

1. 约瑟夫

1.1 方法1

Linear_List_YUSEFUHUAN.c

#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
typedef bool status;
//结点的声明
struct Node
{
	int data;
	struct Node *next;
};
status YueSeFu(int N,int n)//N表示创建多少个,n表示数数数
{
	struct Node *L=(struct Node *)malloc(sizeof(struct Node));//建立第一个结点(不带头结点)
	L->data=1;
	L->next=NULL;
	
	struct Node *rear=L;//找到尾结点,使用尾插法进行插入数据
	struct Node *temp=NULL;
	for(int i=2;i<=N;i++)
	{
		temp=(struct Node *)malloc(sizeof(struct Node));
		temp->data=i;
		temp->next=NULL;	
		rear->next=temp;
		rear=temp;
	}
	rear->next=L;
	struct Node *move=L;
	struct Node *temp1=NULL;
	while(move->next!=move)
	{
		for(int i=2;i<=n;i++)
		{
			temp1=move;
			move=move->next;
		}
		temp1->next=move->next;
		move=move->next;
	}
	printf("胜利的数据:%d\n",move->data);
}
int main()
{
	 YueSeFu(7,3);
	return 0;
}



1.2 方法2

yuesefu.c

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

typedef bool status;

struct Node{
    int num;
    struct Node* next;
};

//初始化创建循环单链表
struct Node* loop_creat(){
    struct Node* p = (struct Node*)malloc(sizeof(struct Node));
    if(p==NULL){
        printf("创建失败\n");
        return NULL;
    }
    p->num = 0;
    p->next = p;
    return p;
}

//头插法
status loop_head_insert(struct Node* p,int data){
    if(p->next==p){//链表为空
        struct Node* new = (struct Node*)malloc(sizeof(struct Node));
        if(new==NULL){
            return false;
        }
        new->num = data;
        p->next = new;
        new->next = p;
        return true;
    }
    //链表中有数据
    struct Node* new = (struct Node*)malloc(sizeof(struct Node));

    new->num = data;
    struct Node* tmp = p->next;
    p->next = new;
    new->next = tmp;
    return true;

}

//遍历链表
status loop_travel(struct Node* p){
    if(p->next == p){//链表为空
        printf("链表为空\n");
        return false;
    }

    struct Node* tmp = p;
    while(tmp->next!=p){
        tmp = tmp->next;
        printf("%d ",tmp->num);
    }
    return true;
}

//删除结点__头删法
status loop_delete_head(struct Node* p){
    if(p->next==p){//链表为空
        printf("链表为空无法删除\n");
        return false;
    }

    struct Node* temp1 = p->next;
    struct Node* temp2 = temp1->next;//后一个节点
    p->next = temp2;
    temp1->next = NULL;
    free(temp1);
    return true;

}

//删除头结点
struct Node* delete_head_node(struct Node* p){
    if(p->next==p){
        printf("链表为空\n");
        return NULL;
    }

    struct Node* tmp = p->next;
    while(tmp->next!=p){//遍历到最后一个节点
        tmp = tmp->next;
    }
    tmp->next = p->next;//直接跳过头结点,使最后一个节点指向第一个节点
    free(p);//释放头结点
    p=NULL;
    return tmp->next;//返回第一个节点地址

}

//不带头结点的遍历
status loop_travel_no_head(struct Node* s){
    if(s==NULL){
        printf("链表为空\n");
        return false;
    }

    struct Node* tmp = s;
    do{
        printf("%d ",tmp->num);
        tmp = tmp->next;
    }while(tmp!=s);

}

//不带头结点统计个数
int loop_ele_sum(struct Node* s){
    if(s==NULL){
        printf("链表为空\n");
        return 0;
    }

    struct Node* tmp = s;
    int count = 0;
    do{
        count++;
        tmp = tmp->next;
    }while(tmp!=s);

    return count;

}

//尾插法
status loop_back_insert(struct Node* p,int data){
    if(p->next==p){//链表为空
        struct Node* new = (struct Node*)malloc(sizeof(struct Node));
        if(new==NULL){
            return false;
        }
        new->num = data;

        p->next = new;
        new->next = p;
        return true;
    }

    struct Node* new = (struct Node*)malloc(sizeof(struct Node));
    if(new==NULL){
        return false;
    }

    struct Node* tmp = p;
    while (tmp->next!=p)
    {
        tmp = tmp->next;

    }
    new->num  = data;
    tmp->next = new;
    new->next = p;
    return true; 

}

//添加循环链表数据
struct Node* loop_add(int num){
    //创建链表
    struct Node* L = loop_creat();

    for(int i=1;i<=num;i++){
        loop_back_insert(L,i);
    }

    //删除头结点
    struct Node* s = delete_head_node(L);
    return s;
}

//约瑟夫
// 1 2 3 4 5 6 7
// 1 2 4 5 6 7
// 1 2 4 5 7
// 1 4 5 7
// 1 4 5
// 1 4
// 4
status yusefu_game(int num,int people){

    struct Node* s = loop_add(people);

    struct Node* cur = s;//当前节点
    struct Node* pre = NULL;//保存前一个节点
    int count=1;
    int ret = loop_ele_sum(s);//

    //遍历
    printf("游戏选手如下\n");
    loop_travel_no_head(s);
    printf("\n");
    printf("开始游戏\n");
    
    while(ret>1){
        count++;
        cur = cur->next;
        if(count==num){

            if(cur==s){//如果我们便利的第一个节点被杀,就将他指向他的下一个节点,方便我们遍历输出
                s = cur->next;
            }
            pre->next = cur->next;//删掉节点

            free(cur);
            cur = pre->next;//cur重新指向被删除节点的下一个节点
            count = 1;

            //看看谁被杀了,谁活着
            loop_travel_no_head(s);
            printf("\n");

            ret--;

        }
        pre = cur;//保存前一个节点

    }

    printf("恭喜你活下来:%d",cur->num);
    
}
int main()
{
    // struct Node* L = loop_creat();


    // printf("\n***************\n");
    // loop_back_insert(L,1);
    // loop_back_insert(L,2);
    // loop_back_insert(L,3);
    // loop_back_insert(L,4);
    // loop_back_insert(L,5);
    // loop_back_insert(L,6);
    // loop_back_insert(L,7);
    
    // //删除头结点
    // struct Node* s = delete_head_node(L);

    //遍历删除头结点后的链表
    // loop_travel_no_head(s);

    // printf("\n***************\n");

    // int ret = loop_ele_sum(s);
    // printf("%d\n",ret);

    printf("\n***************\n");
    int num,people;
    printf("请输入数到几被杀死:");
    scanf("%d",&num);
    printf("请输入几个人参加游戏:");
    scanf("%d",&people);
    yusefu_game(num,people);
    return 0;
}




相关文章:

  • office办公技能|ppt插件使用
  • 前端笔记(四)Flex 布局
  • java WebSocket带参数处理使用
  • 佳明(Garmin) fēnix 7X 增加小睡检测功能
  • 在windows下编译libiconv库
  • 基于JavaWeb+SSM+Vue微信小程序的科创微应用平台系统的设计和实现
  • HarmonyOS--ArkTS(1)--基本语法(1)
  • 【Python网络爬虫入门教程1】成为“Spider Man”的第一课:HTML、Request库、Beautiful Soup库
  • 使用Java实现基数排序算法
  • Windows 和 MacOS 上安装配置ADB(安卓调试桥)
  • 【人生苦短,我学 Python】(5)集合数据类型(set、frozenset)
  • 【小沐学Python】Python实现TTS文本转语音(speech、pyttsx3、百度AI)
  • 理解基于 Hadoop 生态的大数据技术架构
  • c++学习之异常
  • 18.Java程序设计-基于Springboot的电影院售票系统的设计与实现
  • 论文阅读——Deformable ConvNets v2
  • 详细介绍开源固件-TF-A
  • 学习 Vue 3 源码
  • ES6与ES5的区别?
  • 文心一言API(高级版)使用
  • 海航回应“男团粉丝为追星堵住机舱通道”:已紧急阻止
  • “毛茸茸”的画,诗意、温暖又治愈
  • “80后”赵亮出任上海普陀区委副书记
  • 云南一餐馆收购长江野生鱼加工为菜品,被查处罚款
  • 长三角地区中华老字号品牌景气指数发布,哪些牌子是你熟悉的?
  • 洲际酒店:今年第一季度全球酒店平均客房收入同比增长3.3%