Java的置换alghorithm [英] Java permutation alghorithm

查看:453
本文介绍了Java的置换alghorithm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code现在,而且在输入我有升序排序我的名字的字母ahimrsu。我需要从所有组合显示正确的号码为mariush这应该是2170目前只显示ahimrsu,ahimrus,ahimsru,ahimsur,ahimurs,ahimusr,ahirmus,ahirmsu ....等我该怎么办这个?

 <!DOCTYPE HTML>

< HTML>
< HEAD>
<! - 脚本函数从这里开始 - >
<脚本类型=文/ JavaScript的>
        函数烫发(数据){
    如果(!(数据的instanceof阵列)){
        抛出新类型错误(输入数据必须是一个阵列);
    }

    数据= data.slice(); //进行复制
    VAR置换= [],
        堆栈= [];

    功能doPerm(){
        如果(data.length == 0){
            permutations.push(stack.slice());
        }
        对于(VAR I = 0; I< data.length;我++){
            变种X = data.splice(I,1);
            stack.push(X);
            doPerm();
            stack.pop();
            data.splice(ⅰ,0,x)的;
        }
    }

    doPerm();
    返回排列;
}

变种输入=ahimrsu.split('');
VAR的结果=烫发(输入);
对于(VAR I = 0; I< result.length;我++){
    结果[I] =结果[I]。加入('');
}
执行console.log(结果);
< / SCRIPT>
<! - 头从这里开始 - >
< /头>
<身体GT;
<! - 脚本结果 - >
<脚本类型=文/ JavaScript的>
        文件撰写(结果);
< / SCRIPT>

< /身体GT;
< / HTML>
 

解决方案

你提的问题是数学的性质 - 排列组合。你实际上问可能排列的数字符串长度为7。 其计算公式为阶乘(numOfchar)。

在这种情况下,7! = 7x6x5x4x3x2x1 = 5040。

 公共静态无效的主要(字串[] args){

    字符串str =ABCDEFH;

    的System.out.println(+ STR +是:置换为数+阶乘(str.length()));
}


公共静态INT因子(INT N)
{
    如果(N == 0)
        返回1;

    INT结果=因子(N-1)* N;
    返回结果;
}
 

程序输出:

排列为ABCDEFH的数字是:5040

既然你标记的Java ,这是你能得到它与J​​ava做的一种方式。

So I have this code now, and in input I have in ascending order my name's letters "ahimrsu". I need to show up the right number for "mariush" from all combinations which should to be 2170. For now it only show ahimrsu, ahimrus, ahimsru, ahimsur, ahimurs, ahimusr, ahirmus, ahirmsu.... etc How can I do this?

<!DOCTYPE HTML>

<html>
<head>
<!--Script Function Start Here-->
<script type="text/javascript">
        function perms(data) {
    if (!(data instanceof Array)) {
        throw new TypeError("input data must be an Array");
    }

    data = data.slice();  // make a copy
    var permutations = [],
        stack = [];

    function doPerm() {
        if (data.length == 0) {
            permutations.push(stack.slice());
        }
        for (var i = 0; i < data.length; i++) {
            var x = data.splice(i, 1);
            stack.push(x);
            doPerm();
            stack.pop();
            data.splice(i, 0, x);
        }
    }

    doPerm();
    return permutations;
}

var input = "ahimrsu".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
    result[i] = result[i].join('');
}
console.log(result);
</script>
<!--Header start here-->
</head>
<body>
<!--Script Result-->
<script type="text/javascript">
        document.write(result);
</script>

</body>
</html>

解决方案

Your question is of mathematics nature - combinations and permutations. You are actually asking the number of possible permutation for string length 7. The formula is factorial(numOfchar).

In this case 7! = 7x6x5x4x3x2x1 = 5040.

public static void main(String[] args) {

    String str = "ABCDEFH";

    System.out.println("Number of permutations for " + str + " is : " + factorial(str.length()));
}


public static int factorial(int n)
{
    if (n == 0)
        return 1;

    int result = factorial(n-1)*n;
    return result;  
}

Program Output:

Number of permutations for ABCDEFH is : 5040

Since you tagged Java, this is one way you can get the it done with Java.

这篇关于Java的置换alghorithm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆