列出由0123456789取3位组成的无序无重复的所有组合【三字现】

JavaScript William 102浏览 0评论

列出由0123456789取3位组成的无序无重复的所有组合,如012,021,102,120,210,201只算一个012。

Javascript代码如下:

        var data = {},r="";
        for (var i = 0; i <= 9; i++) {
            for (var j = 0; j <= 9; j++) {
                if (i == j) continue;
                for (var k = 0; k <= 9; k++) {
                    if (i == k || j == k) continue;
                    var a = [i, j, k].sort(),b=a.join('');
                    data[b] = b;
                }
            }
        }

        for (var d in data) {
            r += data[d] + ",";
        }
        document.writeln(r);

结果:

123,124,125,126,127,128,129,134,135,136,137,138,139,145,146,147,148,149,156,157,158,159,167,168,169,178,179,189,234,235,236,237,238,239,245,246,247,248,249,256,257,258,259,267,268,269,278,279,289,345,346,347,348,349,356,357,358,359,367,368,369,378,379,389,456,457,458,459,467,468,469,478,479,489,567,568,569,578,579,589,678,679,689,789,012,013,014,015,016,017,018,019,023,024,025,026,027,028,029,034,035,036,037,038,039,045,046,047,048,049,056,057,058,059,067,068,069,078,079,089

更多


var a = ["123", "124", "125", "126", "127", "128", "129", "134", "135", "136", "137", "138", "139", "145", "146", "147", "148", "149", "156", "157", "158", "159", "167", "168", "169", "178", "179", "189", "234", "235", "236", "237", "238", "239", "245", "246", "247", "248", "249", "256", "257", "258", "259", "267", "268", "269", "278", "279", "289", "345", "346", "347", "348", "349", "356", "357", "358", "359", "367", "368", "369", "378", "379", "389", "456", "457", "458", "459", "467", "468", "469", "478", "479", "489", "567", "568", "569", "578", "579", "589", "678", "679", "689", "789", "012", "013", "014", "015", "016", "017", "018", "019", "023", "024", "025", "026", "027", "028", "029", "034", "035", "036", "037", "038", "039", "045", "046", "047", "048", "049", "056", "057", "058", "059", "067", "068", "069", "078", "079", "089"]
        var b = ["0", "1", "3", "6"], bl = b.length, sum = 3, r1 = [], r2 = "";
        for (var i = 0; i < a.length; i++) {
            switch (bl) {
                case 4:
                    sum = a[i].indexOf(b[0]) + a[i].indexOf(b[1]) + a[i].indexOf(b[2]) + a[i].indexOf(b[3]);
                    if (sum == -4) {
                        r1.push(a[i]);
                    }
                    break;
                case 3:
                    sum = a[i].indexOf(b[0]) + a[i].indexOf(b[1]) + a[i].indexOf(b[2]);
                    if (sum == -3) {
                        r1.push(a[i]);
                    }
                    break;
                case 2:
                    sum = a[i].indexOf(b[0]) + a[i].indexOf(b[1]);
                    if (sum == -2) {
                        r1.push(a[i]);
                    }
                    break;
            }
        }
        document.writeln(r1.join(','));
        var rnd = "";
        for (var i = 0; i < 6; i++) {
            var index = Math.floor((Math.random() * r1.length));
            rnd += r1[index] + ",";
        }
        document.writeln(rnd);

转载请注明:AspxHtml学习分享网 » 列出由0123456789取3位组成的无序无重复的所有组合【三字现】

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址