Java--算法--约瑟夫问题

  1. 约瑟夫问题的介绍:
    1.  

       

  2. 代码实现

    1.   
    2. package com.model.linkedlist.circular;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/9 11:21
       * 演示单项环形链表的实现
       */
      public class CircularLinkedListDemo01 {
          public static void main(String[] args) {
              CircularLinkedList list = new CircularLinkedList();
              list.create(5);
              list.show();
              System.out.println("环形队列的个数:"+list.count());
              System.out.println("环形链表的第一个节点"+list.getFirst().toString());
      //        order(list, 2);
              inOrder(list,  1, 2, 5);
              CircularLinkedList list1 = new CircularLinkedList();
              list1.inOrder(1, 5, 10);
      
          }
      //传入一个环形队列模拟约瑟夫问题,给出一出圈的顺序
          public static void order(CircularLinkedList list,int n){
      
              if (list.isEmpty()){
                  System.out.println("链表为空不能出圈");
                  return;
              }
             if (n<1){
                 System.out.println("输入的数据不正确");
                 return;
             }
             Node temp = list.getFirst();
             while(true) {
                 if (temp.next==temp){
                     System.out.println(temp.id);
                     break;
                 }
                 for (int i = 0; i < n-2; i++) {
                     temp = temp.next;
                 }
                 System.out.println(temp.next.id);
                 temp.next = temp.next.next;
                 temp=temp.next;
             }
      
      
      
          }
      //约瑟夫问题的小孩出圈
          public static void inOrder(CircularLinkedList list,int startNo,int countNo,int nums){
      //        startNo:开始计数的节点
      //        countNo:没隔数几个数出圈
      //        nums:一共有几个节点
              if (list.isEmpty()||startNo<0||countNo>nums){
                  return;
              }
              Node first = list.getFirst();
              Node temp=first;
              while(true){ //将temp执行最后一个节点
                  if (temp.next==first){
                      break;
                  }
                  temp=temp.next;
              }
              for (int i=0;i<startNo-1;i++){
                  first=first.next;
                  temp=temp.next;
              }
              while(true){
                  if (temp==first){
                      System.out.println(first.id);
                      break;
                  }
                  for (int i=0;i<countNo-1;i++){
                      first=first.next;
                      temp=temp.next;
                  }
                  System.out.println(first.id);
                  first=first.next;
                  temp.next=first;
              }
          }
      }
      
      class CircularLinkedList {
          private Node first = null;
          public Node getFirst() {
              return first;
          }
      
          public void create(int n) {
              Node temp = first;
              if (n<1){
                  System.out.println("数值不正确");
                  return;
              }
              for (int i=1;i<=n;i++){
                  Node node = new Node(i);
                  if (first==null){ //第一个添加构成环形队列
                      first=node;
                      first.next=first;
                      temp=first;
                  }else {
                      temp.next=node;
                      node.next=first;
                      temp=node;
                  }
              }
          }
      //    遍历整个环形链表
          public void show(){
              if (isEmpty()){
                  System.out.println("链表为空不能进行遍历");
                  return;
              }
              Node temp=first;
              while(true){
                  System.out.println(temp);//first也是一个节点
                  if (temp.next==first){
                      break;
                  }
                  temp=temp.next;
              }
          }
      //    统计环形队列的个数
          public int count(){
              int count=0;
              if (isEmpty()){
                  return 0;
              }else {
                  Node temp=first;
      
                  while(true){
                      count++;
                      if (temp.next==first){
                          break;
                      }
                      temp=temp.next;
                  }
              }
              return count;
          }
          public boolean isEmpty(){
              if (first==null){
                  return true;
              }
              return false;
          }
      
          public  void inOrder(int startNo,int countNo,int nums){
      //        startNo:开始计数的节点
      //        countNo:没隔数几个数出圈
      //        nums:一共有几个节点
              create(nums);
              if (isEmpty()||startNo<0||countNo>nums){
                  return;
              }
              Node first = getFirst();
              Node temp=first;
              while(true){ //将temp执行最后一个节点
                  if (temp.next==first){
                      break;
                  }
                  temp=temp.next;
              }
              for (int i=0;i<startNo-1;i++){
                  first=first.next;
                  temp=temp.next;
              }
              while(true){
                  if (temp==first){
                      System.out.println(first.id);
                      break;
                  }
                  for (int i=0;i<countNo-1;i++){
                      first=first.next;
                      temp=temp.next;
                  }
                  System.out.println(first.id);
                  first=first.next;
                  temp.next=first;
              }
          }
      
      }
      
      class Node {
          public int id;
          public Node next;
      
          public Node(int id) {
              this.id = id;
          }
      
          public Node() {
          }
      
          @Override
          public String toString() {
              return "Node{" +
                      "id=" + id +
                      '}';
          }
      }

       

posted @ 2021-07-09 16:09  张紫韩  阅读(154)  评论(0编辑  收藏  举报