• 全部
  • 问答

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

zedan 2008-03-11 06:04:34
今天看到有人问,觉得有点意思,来这里看下大家有什么好点的算法.
题:单词"character",要求里面有4个字母是大写的,其它小写.列出所有的组合.
请教...
...全文
80 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
顺便说一下,这个在ff下运行
ie下需要修改 str2arr
因为ie不支持 String[i] 怎么取第几个字符的函数忘记了...
回复
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">
<head>
<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);
// alert ('go');
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>
</head>
<body>
<a href="javascript:main()">Exec</a>
</body>
</html>
回复
指间的风 2008-03-11
这个太难了
回复
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
创建于2007-09-28

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2008-03-11 06:04
社区公告
暂无公告