• 全部
• 问答

# 求个算法. 应该个是排列问题

zedan 2008-03-11 06:04:34

...全文
80 点赞 收藏 8

8 条回复

myvicy 2008-03-12
``````<script>
var mystr = "character".split("");
var a=[];
function comb(m,k){
var   i,j;
for(i=m;i>=k;i--){
a[k]=i;
if(k>1)   comb(i-1,k-1);
else{
for(n=1;n<mystr.length+1;n++){
w = false;
for(j=a[0];j>0;j--){
if(a[j]==n){
document.write(mystr[n-1].toUpperCase());
w = true;
}
}
if(!w)document.write(mystr[n-1]);
}
document.write("<br>");
}
}
}

function main(){
l = 4
a[0]=l;
comb(mystr.length,l);
}
main();
</script>``````

charaCTER
charAcTER
chaRacTER
chAracTER
cHaracTER
CharacTER
charACtER
chaRaCtER
chAraCtER
cHaraCtER
CharaCtER
chaRActER
chArActER
cHarActER
CharActER
chARactER
cHaRactER
ChaRactER
cHAractER
ChAractER
CHaractER
charACTeR
chaRaCTeR
chAraCTeR
cHaraCTeR
CharaCTeR
chaRAcTeR
chArAcTeR
cHarAcTeR
CharAcTeR
chARacTeR
cHaRacTeR
ChaRacTeR
cHAracTeR
ChAracTeR
CHaracTeR
chaRACteR
chArACteR
cHarACteR
CharACteR
chARaCteR
cHaRaCteR
ChaRaCteR
cHAraCteR
ChAraCteR
CHaraCteR
chARActeR
cHaRActeR
ChaRActeR
cHArActeR
ChArActeR
CHarActeR
cHARacteR
ChARacteR
CHaRacteR
CHAracteR
charACTEr
chaRaCTEr
chAraCTEr
cHaraCTEr
CharaCTEr
chaRAcTEr
chArAcTEr
cHarAcTEr
CharAcTEr
chARacTEr
cHaRacTEr
ChaRacTEr
cHAracTEr
ChAracTEr
CHaracTEr
chaRACtEr
chArACtEr
cHarACtEr
CharACtEr
chARaCtEr
cHaRaCtEr
ChaRaCtEr
cHAraCtEr
ChAraCtEr
CHaraCtEr
chARActEr
cHaRActEr
ChaRActEr
cHArActEr
ChArActEr
CHarActEr
cHARactEr
ChARactEr
CHaRactEr
CHAractEr
chaRACTer
chArACTer
cHarACTer
CharACTer
chARaCTer
cHaRaCTer
ChaRaCTer
cHAraCTer
ChAraCTer
CHaraCTer
chARAcTer
cHaRAcTer
ChaRAcTer
cHArAcTer
ChArAcTer
CHarAcTer
cHARacTer
ChARacTer
CHaRacTer
CHAracTer
chARACter
cHaRACter
ChaRACter
cHArACter
ChArACter
CHarACter
cHARaCter
ChARaCter
CHaRaCter
CHAraCter
cHARActer
ChARActer
CHaRActer
CHArActer
CHARacter

zedan 2008-03-12
* myvicy 和 zwd2005两位大哥得到的结果数量就不同, 哪个是正确的.

zwd2005 2008-03-11
``````function str2arr (str) {
var arr = [];
for (var i=0; i<str.length; i++) {
arr.push(str.charAt(i));
}
return arr;
}
``````

zwd2005 2008-03-11

ie下需要修改 str2arr

zwd2005 2008-03-11
``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Create-Date" content="Feb 28, 2008" />
<title>Test</title>
<script type="text/javascript">
var gen = function (arr, length, n) {
//	document.write ('length=' + length + '; n=' + n + '; ');
//	show (arr);
if (length == n) {
for (var i=0; i<length; i++) {
arr[i] = arr[i].toUpperCase();
}
show(arr);
return ;
}
if (length > n) {
arr[length-1] = arr[length-1].toLowerCase();
gen(arr, length-1, n);
}
if (n > 0) {
arr[length-1] = arr[length-1].toUpperCase();
gen(arr, length-1, n-1);
}
}

function show(arr) {
for (var i=0; i<arr.length; i++) {
document.write(arr[i]);
}
document.write("<br />");
}

function str2arr (str) {
var arr = [];
for (var i=0; i<str.length; i++) {
arr.push(str[i]);
}
return arr;
}

function main() {
var arr = str2arr('character');
gen (arr, arr.length, 4);
document.close();
}

</script>
<body>
<a href="javascript:main()">Exec</a>
</body>
</html>``````

myvicy 2008-03-11
``````var a=[];
function comb(m,k){
var   i,j;
for(i=m;i>=k;i--){
a[k]=i;
if(k>1)   comb(i-1,k-1);
else{
for   (j=a[0];j>0;j--)   document.write(a[j]);
document.write("\n");
}
}
}

function main(){
a[0]=4;
comb(9,4);
}
main();``````

9876 9875 9874 9873 9872 9871 9865 9864 9863 9862 9861 9854 9853 9852 9851 9843 9842 9841 9832 9831 9821 9765 9764 9763 9762 9761 9754 9753 9752 9751 9743 9742 9741 9732 9731 9721 9654 9653 9652 9651 9643 9642 9641 9632 9631 9621 9543 9542 9541 9532 9531 9521 9432 9431 9421 9321 8765 8764 8763 8762 8761 8754 8753 8752 8751 8743 8742 8741 8732 8731 8721 8654 8653 8652 8651 8643 8642 8641 8632 8631 8621 8543 8542 8541 8532 8531 8521 8432 8431 8421 8321 7654 7653 7652 7651 7643 7642 7641 7632 7631 7621 7543 7542 7541 7532 7531 7521 7432 7431 7421 7321 6543 6542 6541 6532 6531 6521 6432 6431 6421 6321 5432 5431 5421 5321 4321

JavaScript

8.5w+

Web 开发 JavaScript

2008-03-11 06:04