Alk*_*tel 3 algorithm permutation combinatorics
例如,
rank permutation
0 abc
1 acb
2 bac
3 bca
4 cab
5 cba
Run Code Online (Sandbox Code Playgroud)
所以,如果有人要求我给排名4排列,答案就是出租车.请给出这个程序的java代码
我是第一次尝试!! :-)真的很好的功课,很好的问题,你让我的一天!这是javascript中的解决方案:
function permutation (rank, n, chars)
{
var fact, char_idx, this_char;
if (n == 0)
return "";
char_idx = Math.floor(rank / factorial(n - 1));
this_char = chars.splice(char_idx, 1);
// returns the char with index char_idx and removes it from array
return this_char +
permutation(rank % factorial(n - 1), n - 1, chars);
}
Run Code Online (Sandbox Code Playgroud)
只是称之为就是这样permutation(5, 3, ['a', 'b', 'c'])
.你必须编写自己的factorial()函数 - 作为作业:-)