首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Josephus队列-棘手的问题

Josephus队列-棘手的问题
EN

Stack Overflow用户
提问于 2014-04-21 09:14:11
回答 1查看 2.7K关注 0票数 1

我正在尝试使用java中的队列来解决Josephus问题。输入必须是名称列表(字符串)和整数k或土豆通过队列的次数。我需要测试4个不同的输入。这就是我到目前为止所做的,我从一些随机的教程中得到了帮助,但不确定要编辑什么才能让它工作。

代码语言:javascript
复制
 /*Jess Anastasio
  * HW 1
  * Part 2
  */ 

 import java.util.*;
 import java.util.Queue;
    /*pseudocode:
     * int k = amount of people cycled through 
     * before deleted. Need to enter string input
     * of names into queue. Need to cycle through
     * the queue k times and the object at position
     * k will be deleted. 
     * 
     * first: create string array holding names
     * method buildQueue takes the string array 
     * and adds it to a string queue
     * next: Josephus method passes in string queue and
     * int k, 
     */

  public class HotPotato 
  {
   //tester
    public static void main(String[] args) 
     {
    //use an array to save string input of names (4 different inputs)
       String[] a1 = {"Genna", "Bob", "Rob", "Alexa", "Brian", "Frank"};
       String[] a2 = {"Rich", "Taylor", "Jackie", "Jan", "Kim", "Denver"};
       String[] a3 = {"Jess", "Kyle", "Mike", "Carolyn", "Ant", "Ed"};
       String[] a4 = {"Natalie", "Emma", "Olivia", "Joe", "Kitty", "Jenn"};
       System.out.println("First winner is " + Josephus(buildQueue(a1), 5));
       System.out.println("Second winner is " + Josephus(buildQueue(a2), 2));
       System.out.println("Third winner is " + Josephus(buildQueue(a3), 4));
       System.out.println("Fourth winner is " + Josephus(buildQueue(a4), 10));
     }

    // build a queue from an array of string objects
    public static Queue<String> buildQueue(String[] str) 
    {
      Queue<String> Q = new Queue<String>();
      for (int i = 0; i < str.length; i++)
        Q.enqueue(str[i]);

      return Q;
    }

     // Josephus problem using a queue
     public static String Josephus(Queue<String> Q, int k) 
     {
       //if there is nothing in the queue return null
       if (Q.isEmpty()) 
        return null;

       //while the size of the queue is greater than 1
       while (Q.size() > 1) 
       {
         //print out the contents of the queue
         System.out.println(" Queue: " + Q + " k = " + k);
         for (int i = 1; i < k; i++)
           Q.enqueue(Q.dequeue());
         String e = Q.dequeue();
         //update user on who was deleted 
         System.out.println(" " + e + " is out");
       }
       return Q.dequeue();
     }

  }

这不管用..有人能给点建议吗?

EN

回答 1

Stack Overflow用户

发布于 2014-04-21 11:07:18

我不确定你的while循环。这就是我想到的-- this.potentialVictims是一个队列

代码语言:javascript
复制
public String findSurvivor(int k) { 

  int pos=1;
  while (this.potentialVictims.size() > 1) {
    String person=this.potentialVictims.poll();
    if (pos++ %k != 0) {
            this.potentialVictims.offer(person);
    }
    else {
       System.out.printf("%s is out\n",person);
    }
  }
  if (this.potentialVictims.size()==1) {
    return this.potentialVictims.poll();
  }
  else {
    return null;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23189420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档