gpt4 book ai didi

Java - 使用递归查找单词的排列?

转载 作者:行者123 更新时间:2023-11-29 07:14:55 25 4
gpt4 key购买 nike

我有一个程序,它会找到作为命令行参数给出的单词的所有可能排列,但无法从程序中获得任何输出,程序编译正常,当我运行程序时,我看不出有什么问题。有任何想法吗?

    import java.io.*;
02
03 public class Anagrams
04 {
05 private static char [] word;
06 private static char [] permutation;
07 private static boolean [] characterUsed;
08
09
10
11 public static void main(String [] args)throws Exception
12 {
13
14 word = args[0].toCharArray();
15 permutation = new char[word.length];
16 characterUsed = new boolean[word.length];
17 printPermutations(0);
18 }//main
19
20 private static void printPermutations(int currentIndex)throws Exception
02 {
03
04 if(currentIndex == permutation.length)
05 System.out.println(permutation);
06 else
07 {
08 for(int index=0;index<word.length-1;index++)
09 {
10 //if the character at that index hasn't been used
11 if(!characterUsed[index]);
12 {
13 //mark character at this position as in use
14 characterUsed[index] = true;
15 //put the character in the permutation
16 permutation[index]= word[currentIndex];
17 printPermutations(currentIndex +1);
18 characterUsed[index] = false;
19 }//if
20 }//for
21 }//else
22 }//printPermutation
41 }//Anagrams

最佳答案

不确定这是否是唯一的问题,但这行看起来也有问题:

for (int index = 0; index < argument.length - 1; index++)

您的意思是不使用数组中的最后一个 char 吗?你可能是说:

for (int index = 0; index <= argument.length - 1; index++)

for (int index = 0; index < argument.length; index++)

关于Java - 使用递归查找单词的排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com