我想计算所有可能的输入单词的字谜,我想添加计数所有字谜的计数器
代表。字符串农场有28个可能的字谜 我想要这个输出
输入字符串:abc
这里是所有的字谜:abc
ABC ACB BAC BCA CBA 驾驶室
字符串“abc”有6个可能的字谜
“这是代码”
import java.util.Scanner;
public class Anagrams1 {
public static void main (String args[]) {
Scanner r = new Scanner(System.in);
System.out.print("Enter a string:");
String s = r.next();
char[] text = new char[s.length()];
for (int i=0; i<s.length(); i++)
text[i] = s.charAt(i);
System.out.println("Here are all the anagrams of " + s);
makeAnagram(text,0);
System.out.println("Goodbye!");
}
static void makeAnagram(char[] a, int i) {
if (i == a.length-1) {
printArray(a);
}
else {
for (int j=i; j< a.length; j++) {
char c = a[i];
a[i] = a[j];
a[j] = c;
makeAnagram(a, i+1);
c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
static void printArray(char [] a)
{
for (int i=0; i< a.length; i++)
System.out.print(a[i]);
System.out.println();
}
}
答案 0 :(得分:0)
如果您要计算输入str
的字谜数量,则该数字为factorial(str.length())
public static final int factorial(int base) {
return (base == 1) ? (1) : (factorial(base - 1));
}