Josephus问题(循环链表)

假如有M个人想用以下方法来选出一个领袖:M个人围成一个圈,从某个人开始按顺序数数,每次数到N那个人出局,最后仅存的那个人就是领袖。
用循环链表来表示这M个人,不断循环链表,每当数到N,那个结点去掉,直到只剩一个结点为止。
class Node
    {
        public int index;//表示第几个人
        public Node next;
        public Node(int x, Node t)
        {
            index= x;
            next = t;
        }
    }

    class Program
    {
        //用尾插法(新插入的结点总在最后)建一个循环链表
        static Node CreateLister(int m)
        {
            Node head,s,r;
            //第一个结点
            head = new Node(1, null);
            head.next = head;
            //尾指针
            r = head;
            for (int i = 2; i <= m; i++)
            {
                s = new Node(i, head);//新结点,指向头指针
                r.next = s;//新结点插入表尾
                r = s;//尾指针指向新的表尾
            }
            r.next = head;//表尾的下一个接着表头,形成循环
            return r;//返回尾指针
        }

        static void Main(string[] args)
        {         
            const int M = 9;
            const int N = 5;
            Node x = CreateLister(M);

            while (x != x.next)
            {
                //跳到出局的人的前一个人
                for (int i = 1; i < N; i++)
                {
                    x = x.next;
                }
                Console.Write(x.next.item.ToString() + ",");
                x.next = x.next.next;//第N个人出局
            }
            Console.Write(x.item.ToString());
            Console.Read();          
        }
    }

转载于:https://www.cnblogs.com/guoxiaocong/archive/2005/12/27/306066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环链表是解决约瑟夫问题的一种常见方法。下面是一个使用Java循环链表解决约瑟夫问题的示例: ```java class Node { int data; Node next; public Node(int data) { this.data = data; } } class CircularLinkedList { private Node head; private Node tail; public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; tail.next = head; } else { tail.next = newNode; tail = newNode; tail.next = head; } } public void remove(Node node) { if (head == null) { return; } if (head == node) { head = head.next; tail.next = head; } else { Node current = head; while (current.next != node) { current = current.next; } current.next = node.next; if (node == tail) { tail = current; tail.next = head; } } } public void josephus(int k) { Node current = head; while (current.next != current) { for (int i = 1; i < k; i++) { current = current.next; } System.out.print(current.data + " "); remove(current); current = current.next; } System.out.println(current.data); } } public class Main { public static void main(String[] args) { CircularLinkedList list = new CircularLinkedList(); int n = 7; // 总人数 int k = 3; // 报数为3的人出列 for (int i = 1; i <= n; i++) { list.add(i); } System.out.println("出列顺序:"); list.josephus(k); } } ``` 这段代码中,我们首先定义了一个`Node`类来循环链表的节点,然后定义了`CircularLinkedList`类来实现循环链表的操作。在`josephus`方法中,我们使用循环链表模拟约瑟夫问题的解决过程,每次找到第k个节点并移除,直到只剩下一个节点为止。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值