gpt4 book ai didi

java - 查找java中单词排列的方式数

转载 作者:行者123 更新时间:2023-12-02 06:56:34 25 4
gpt4 key购买 nike

我正在用 java 编写一个程序,它将计算一个单词可以排列/重新排列的方式数量。

例如,单词“HAPPY”可以以 60 种方式重新排列。

我的过程是找到单词长度的阶乘,然后将其除以单词中出现的不同字母的阶乘的乘积。(使用递归)。

这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
public static int factorial(int fact)
{
if(fact == 0)
return 1;
else if(fact == 0)
return 1;
return(fact * (factorial(fact - 1)));

}
public static void main(String args[]) throws IOException
{
String word;
int result;
int cases,lengthOfWord;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
cases = Integer.parseInt(br.readLine());
for(int iteration =1;iteration <= cases; iteration++)
{
word = br.readLine();
lengthOfWord = word.length();
a = lengthOfWord - word.replace("A", "").length();
b = lengthOfWord - word.replace("B", "").length();
c = lengthOfWord - word.replace("C", "").length();
d = lengthOfWord - word.replace("D", "").length();
e = lengthOfWord - word.replace("E", "").length();
f = lengthOfWord - word.replace("F", "").length();
g = lengthOfWord - word.replace("G", "").length();
h = lengthOfWord - word.replace("H", "").length();
i = lengthOfWord - word.replace("I", "").length();
j = lengthOfWord - word.replace("J", "").length();
k = lengthOfWord - word.replace("K", "").length();
l = lengthOfWord - word.replace("L", "").length();
m = lengthOfWord - word.replace("M", "").length();
n = lengthOfWord - word.replace("N", "").length();
o = lengthOfWord - word.replace("O", "").length();
p = lengthOfWord - word.replace("P", "").length();
q = lengthOfWord - word.replace("Q", "").length();
r = lengthOfWord - word.replace("R", "").length();
s = lengthOfWord - word.replace("S", "").length();
t = lengthOfWord - word.replace("T", "").length();
u = lengthOfWord - word.replace("U", "").length();
v = lengthOfWord - word.replace("V", "").length();
w = lengthOfWord - word.replace("W", "").length();
x = lengthOfWord - word.replace("X", "").length();
y = lengthOfWord - word.replace("Y", "").length();
z = lengthOfWord - word.replace("Z", "").length();
result = factorial(lengthOfWord) / (factorial(a)*factorial(b)*factorial(c)*factorial(d)*factorial(e)*factorial(f)*factorial(g)*factorial(h)*factorial(i)*factorial(j)*factorial(k)*factorial(l)*factorial(m)*factorial(n)*factorial(o)*factorial(p)*factorial(q)*factorial(r)*factorial(s)*factorial(t)*factorial(u)*factorial(v)*factorial(w)*factorial(x)*factorial(y)*factorial(z));
System.out.printf("Data set %d: %d\n",iteration,result);
}
}
}

但我认为它非常冗长且效率低下。

如何使这个程序更短、更高效?

我还想知道解决这个程序的其他方法。

请帮忙。谢谢。

最佳答案

您可以将字母计数保存在长度为 26 的数组中。此外,用于检查字符计数的字符串替换过于复杂。只需循环遍历字符并计算它们即可。 letters[word.charAt(j) - 'A'] 构造是一种诡计,它使 A 的计数出现在索引 0 处,B 出现在索引 1 处,等等。使用循环将阶乘相乘。并将单词转换为大写。最后,始终声明变量尽可能接近实际使用的位置。 (最后一个只是一般的良好实践。)

把它们放在一起:

public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
for(int i = 0; i < cases; i++)
{
int letters = new int[26];
String word = br.readLine().toUpperCase();
int lengthOfWord = word.length();
for (int j = 0; j < lengthOfWord; j++)
{
letters[word.charAt(j) - 'A']++;
}
int factorialProduct = 1;
for (int j = 0; j < letters.length; j++)
{
factorialProduct *= factorial(letters[j]);
}
int result = factorial(lengthOfWord) / factorialProduct;
System.out.printf("Data set %d: %d\n",iteration,result);
}
}

关于java - 查找java中单词排列的方式数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256572/

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