java中遇到的类似约瑟夫问题

暑假中学习java过程中遇到的类似的约瑟夫问题,由于java没有指针, 所以有数组实现.

/*问题描述:
 *假设M位食客围坐圆桌边,按顺时针依次编号。从begin(begin<=M)号食客开始按顺时针不断报数,
 *报数时依次从1~N,报到N的食客离席,由下位食客继续从1开始报数,最后留下的那位请客。
 *例如M=5,N=3,begin=1时,将由4号食客请客;M=5,N=3,begin=1时,将由1号食客请客。(N不一定小于M)
 *
 *程序主要运用数组进行模拟,通过不断的循环,实现数组中只有一个元素不为0,则该元素的值就是
 *食客的编号。
 *该问题类似约瑟夫问题,这是不用C++中的结构类型和指针来实现的一种代码。
 *Finish at 2005/08/06
*/
import java.io.*;
public class RoundTable{
 public static void main(String args[]){
  //M表示人数
  int M;
  //N表示报数得最大值
  int N;
  //begin表示开始报数的位置
  int begin;
  //进入循环,当用户输入M的值小于0时,退出
  while(true){
  try{
  System.out.println("if you want to end the program,you should initialize the M

<=0");
  System.out.print("Please input the M:");
  BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  String input0=in.readLine();
  M=Integer.parseInt(input0);
  if(M<=0)break;//当M<=0,退出
  System.out.print("Please input the N:");
  String input1=in.readLine();
  N=Integer.parseInt(input1);
  System.out.print("Please input the begin:");
  String input2=in.readLine();
  begin=Integer.parseInt(input2);
  }catch(Exception exc){
   System.out.println("Input Error!");
   continue;
  }if(N<=0||begin<=0){
   System.out.println("N or begin can't be the NEGATIVE!");
   continue;    //当N或begin小于0时,重新输入
  }
  //创建一个长度为M的数组
  int []array=new int[M];
  //初始化数组,使数组元素的值为1~M
  for(int i=0;i<M;i++){
   array[i]=i+1;
   }
  //声明变量count1,count2作为计数器
  int count1,count2,check=0;
  count1=1;
  //count2的值初始化为开始的位置
  count2=begin;
  //进入循环,直到check=M-1
  while(true){
   if(count1%N==0&&array[count2-1]!=0){
    array[count2-1]=0;
    check++;
    }
   count2++;
   if(count2>M)count2=count2-M;
   if(array[count2-1]!=0)count1++;
   if(M-check==1)break;
   }
  //输出
  for(int i=0;i<M;i++){
   if(array[i]!=0)System.out.println("该由第"+array[i]+"食客请客!");
   }
  }
 System.out.println("The End!/nThank You For Using!");
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,约瑟夫问题是一个经典的数学问题,描述如下: 有n个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续报数,直到剩下最后一个人。 现在我们可以使用Java的链表来实现这个问题。具体实现步骤如下: 1. 定义一个节点,包含一个数据域和一个指向下一个节点的指针域。 ```java class Node { int data; Node next; public Node(int data) { this.data = data; } } ``` 2. 创建一个循环链表,并初始化数据。 ```java int n = 10; // 总人数 int m = 3; // 报数到m的人出圈 Node head = new Node(1); Node cur = head; for (int i = 2; i <= n; i++) { cur.next = new Node(i); cur = cur.next; } cur.next = head; // 将链表首尾相连,形成循环链表 ``` 3. 开始报数,找到出圈的人,重置链表。 ```java while (cur.next != cur) { // 当链表只剩最后一个节点时结束循环 for (int i = 1; i < m; i++) { // 报数 cur = cur.next; } System.out.println(cur.next.data + "出圈"); // 输出出圈人的编号 cur.next = cur.next.next; // 删除出圈的人 } System.out.println("最后一个人的编号:" + cur.data); // 输出最后一个人的编号 ``` 完整代码如下: ```java public class Josephus { public static void main(String[] args) { int n = 10; // 总人数 int m = 3; // 报数到m的人出圈 Node head = new Node(1); Node cur = head; for (int i = 2; i <= n; i++) { cur.next = new Node(i); cur = cur.next; } cur.next = head; // 将链表首尾相连,形成循环链表 while (cur.next != cur) { // 当链表只剩最后一个节点时结束循环 for (int i = 1; i < m; i++) { // 报数 cur = cur.next; } System.out.println(cur.next.data + "出圈"); // 输出出圈人的编号 cur.next = cur.next.next; // 删除出圈的人 } System.out.println("最后一个人的编号:" + cur.data); // 输出最后一个人的编号 } } class Node { int data; Node next; public Node(int data) { this.data = data; } } ``` 希望能够帮助到你,如有疑问请随时追问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值