约瑟夫环 码蹄集

题目来源:码蹄集

题目描述:

在这里插入图片描述
在这里插入图片描述

大概思路:

1.先构建一个长度为n的循环链表,每个节点对应一个人,节点编号从1到n。

2.从节点1开始,按照题目要求依次报数,当报数达到9时,将当前节点从链表中删除。

3.重复步骤2,直到链表中只剩下一个节点,即为幸存者。

4.如果选择的位置恰好是幸存者的位置,则返回该位置的编号;否则,重新构建循环链表,并从第i个节点开始执行步骤1-3,直到找到幸存者。

Python代码实现:

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

n, m = map(int, input().split())

# 构造循环链表
head = last = Node(1)
for i in range(2, n+1):
    node = Node(i)
    last.next = node
    last = node
last.next = head

i = 1
while head.next != head:
    if i == m:
        p = head
        last.next = head.next
        del p
        i = 1
        head = last.next
    else:
        last = head
        head = head.next
        i += 1

print(head.value)

Java代码实现:

import java.util.Scanner;

class Node {
    int value;
    Node next;

    public Node(int value, Node next) {
        this.value = value;
        this.next = next;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        Node head = null, last = null;
        Node node;

        for (int i = 0; i < n; i++) {
            node = new Node(i + 1, null);
            if (i == 0) {
                head = node;
                last = node;
            } else {
                last.next = node;
                last = node;
            }
        }
        last.next = head;

        int i = 1;
        while (head.next != head) {
            if (i == m) {
                Node p = head;
                last.next = head.next;
                i = 1;
                head = last.next;
                p = null; // 释放节点内存
            } else {
                last = head;
                head = head.next;
                i++;
            }
        }

        System.out.println(head.value);
    }
}

C++代码实现:

参考链接:https://blog.csdn.net/matiji2008/article/details/126072827

#include <iostream>
#include <stdio.h>
#include <memory>
 
typedef struct NODE{
    int value;
    NODE *next;
} NodeList;
 
int main() {
    int n, m;
    scanf("%d %d", &n, &m);
 
    NodeList *head, *last = NULL;
    NodeList *node;
 
    for(int i=0; i<n; ++i){
        node = (NodeList*)malloc(sizeof(NodeList));
        node->value = i+1;
        if(i == 0){
            head = node;
            last = node;
        }else{
            last -> next = node;
            last = node;
        }
    }
    node->next = head;
    last = node;
 
    int i = 1;
    while(head->next != head){
        if(i == m){
            NodeList *p = head;
            last->next = head->next;
            free(p);
            i = 1;
            head = last->next;
        }else{
            last = head;
            head = head->next;
            ++i;
        }
    }
 
    printf("%d\n", head->value);
    return 0;
}

代码提交测试结果:

在这里插入图片描述
附B站老师讲解链接:https://www.bilibili.com/video/BV1Ua4y1V7qX/?t=2324.0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Magneto_万磁王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值