【问题标题】:Permuations Generator排列生成器
【发布时间】:2015-02-26 02:15:48
【问题描述】:

我正在尝试将用户的输入添加到我的排列列表中,但是当我接受用户输入时,程序只是继续运行。在第三次输入后按回车键时,我没有得到任何排列。这是我的代码:

  import java.util.ArrayList;
  import java.util.Scanner;

 /**
 This program demonstrates the permutation generator.
 */
 public class PermutationGeneratorDemo
 {
 public static void main(String[] args)
 {
   Scanner scan = new Scanner(System.in);

   System.out.println("Please enter a 4 letter word: ");
   String word1 = scan.next();
   System.out.println("Enter a 5 letter word: ");
   String word2 = scan.next();
   System.out.println("Enter a 6 letter word: ");
   String word3 = scan.next();


  PermutationGenerator generator = new PermutationGenerator(word1 + word2 + word3);
  ArrayList<String> permutations = generator.getPermutations();
  for (String s : permutations)
  {         
     System.out.println(s);
    }
   }
  }

排列代码:

import java.util.ArrayList;

/**
This class generates permutations of a word.
*/
 public class PermutationGenerator
 {
  private String word;

 /**
  Constructs a permutation generator.
  @param aWord the word to permute
 */
 public PermutationGenerator(String aWord)
{
    word = aWord;
}

PermutationGenerator(String[] wordList) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

 /**
    Gets all permutations of a given word.
   */
 public ArrayList<String> getPermutations()
 {
  ArrayList<String> permutations = new ArrayList<String>();

  // The empty string has a single permutation: itself
  if (word.length() == 0) 
  { 
     permutations.add(word); 
     return permutations; 
  }

  // Loop through all character positions
  for (int i = 0; i < word.length(); i++)
  {
     // Form a simpler word by removing the ith character
     String shorterWord = word.substring(0, i)
           + word.substring(i + 1);

     // Generate all permutations of the simpler word
     PermutationGenerator shorterPermutationGenerator 
           = new PermutationGenerator(shorterWord);
     ArrayList<String> shorterWordPermutations 
           = shorterPermutationGenerator.getPermutations();

     // Add the removed character to the front of
     // each permutation of the simpler word, 
     for (String s : shorterWordPermutations)
     {
        permutations.add(word.charAt(i) + s);
     }
  }
  // Return all permutations
  return permutations;
 }
}

【问题讨论】:

  • 如果您包含实际生成排列的代码会有所帮助...
  • @AmirAfghani 是正确的,只是想我会补充一点:Scanner#next 读取下一个String 默认由space 分隔。 Scanner#nextLine 读入由new-line 字符分隔的下一行。
  • 排列代码现在也在那里。我对Java相当陌生。当我将 next() 更改为 nextLine() 时,我仍然遇到同样的问题。
  • 您的算法不会生成排列。修改您的程序以接受 1 个单词并输入“aaaa”。看看你得到了什么......如果这不是学术练习,请考虑使用 Guava 的 Collections2.permutations 方法

标签: java arrays input arraylist java.util.scanner


【解决方案1】:

我之前认为问题是无限递归,但经过进一步检查,您的程序确实正确终止。但是,您必须意识到生成排列列表是阶乘复杂性。 4+5+6 = 15. 15!是一个很大的数字,1.3076744e+12 来自 Google 计算器,这就是为什么您的程序似乎永远不会结束。

生成这么多字符串需要一段时间。

尝试使用输入 abc 运行程序,您会发现它可以工作,因为它只需要生成 3! = 6 个字符串。

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我一开始也是这么想的,但是当他的代码进入他的getPermutations 方法时,它会陷入无限循环。我没有投反对票,不知道是谁投的。
    • 显然是一个不知道自己并且无法在团队中讨论和解决问题的用户 ;)
    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多