日期:2014-05-16 浏览次数:20405 次
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();
------解决方案--------------------
<!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>