Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找java中单词的排列方式_Java_Permutation - Fatal编程技术网

查找java中单词的排列方式

查找java中单词的排列方式,java,permutation,Java,Permutation,我正在用java编写一个程序,它将计算一个单词可以被排列/重新排列的方式 例如,“快乐”一词可以用60种方式重新排列 我的过程是找到单词长度的阶乘,然后除以单词中出现的不同字母的阶乘(使用递归) 这是我的密码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static int factoria

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

例如,“快乐”一词可以用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);
    }
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共班机
{
公共静态整数阶乘(整数事实)
{
如果(事实==0)
返回1;
else if(事实==0)
返回1;
收益(事实*(阶乘(事实-1));
}
公共静态void main(字符串args[])引发IOException
{
字符串字;
int结果;
国际案例,纵向;
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=新的BufferedReader(新的InputStreamReader(System.in));
cases=Integer.parseInt(br.readLine());

对于(int iteration=1;iteration您不使用循环范围之外的字母,也不一起使用,因此您可以只使用单个变量

String word;
int result;
int cases;
int lengthOfWord;
for(int iteration = 1;iteration <= cases; iteration++) {
    word = br.readLine();
    lengthOfWord = word.length();
    result = factorial(lengthOfWord);
    for (int i = 0; i < letters.length; i++) {
        int divisor = lengthOfWord - word.replace(((char)((int)'A' + i)).toString(), "").length();
        result /= divisor;
    }
System.out.printf("Data set %d: %d\n", iteration, result);
字符串字;
int结果;
int案例;
int Lengthofward;

对于(int iteration=1;iteration您可以将字母计数保留在长度为26的数组中。此外,检查字符计数的字符串替换过于复杂。只需循环检查字符并对其进行计数。
字母[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);
    }
}
publicstaticvoidmain(字符串args[])引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int cases=Integer.parseInt(br.readLine());
对于(int i=0;i