这是我的代码:
<TITLE>Permutation Generator</TITLE>
<link rel="stylesheet" type="text/css" href="standard.css">
<SCRIPT language=JavaScript SRC="feedback.js"></SCRIPT>
<script language=JavaScript>
function count()
{
var number = parseInt(calc.number.value, 10);
var select = parseInt(calc.select.value, 10);
// check input
if (isNaN(number)||(number <= 0)||isNaN(select)||(select <= 0)||(select > number))
{
calc.permutations.value = "Error";
return;
}
var c = Math.round(permutations(number, select));
calc.permutations.value = c;
}
// list permutations in a separate window
function listing()
{
var number = parseInt(calc.number.value);
var select = parseInt(calc.select.value);
// check input
if (isNaN(number)||(number <= 0)||isNaN(select)||(select <= 0)||(select > number))
{
calc.permutations.value = "Error";
return;
}
var c = Math.round(permutations(number, select));
calc.permutations.value = c;
msgWindow = window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
msgWindow.document.open();
msgWindow.document.writeln("<html><head><title>Listing</title></head>");
msgWindow.document.writeln("<body>");
msgWindow.document.writeln("<p><tt><sub>" + number + "</sub>P<sub>" + select + "</sub> = " + c + "</tt>");
msgWindow.document.writeln("<p>listing of permutations");
msgWindow.document.writeln("<p>");
msgWindow.document.writeln("<pre>");
value = new Array()
var a = new Array(); // initialize
for (i = 0; i < select; i++)
a[i] = i + 1; // 1 - 2 - 3 - 4 - ...
while (true)
{
for (n = 0; n < select; n++)
value[n] = a[n];
// for this combination, list permutations in lexicographical order
put_next(select);
while (get_next(select))
put_next(select);
// generate next combination in lexicographical order
i = select - 1; // start at last item
while (a[i] == (number - select + i + 1)) // find next item to increment
--i;
if (i < 0) break; // all done
++a[i]; // increment
for (j = i + 1; j < select; j++) // do next combination
a[j] = a[i] + j - i;
}
msgWindow.document.writeln("</pre>");
msgWindow.document.writeln("<p>All done!");
msgWindow.document.writeln("</body></html>");
msgWindow.document.close();
}
// compute the number of permutations of r objects from a set on n objects
function permutations( n, r )
{
var c = parseInt("1");
if (r > n) return 0;
var d = n - r;
while (n > 0)
{
c = c * n;
--n;
}
while (d)
{
c = c / d;
--d;
}
return c;
}
var value;
// compute the next permutation given the current permutation
function get_next( k )
{
var i = k - 1;
while (value[i-1] >= value[i])
i--;
if (i < 1) return false; // all in reverse order
var j = k;
while (value[j-1] <= value[i-1])
j--;
swap(i - 1, j - 1);
i++;
j = k;
while (i < j)
{
swap(i - 1, j - 1);
i++;
j--;
}
return true;
}
function swap( a, b )
{
temp = value[a];
value[a] = value[b];
value[b] = temp;
}
function put_next( k )
{
for (i = 0; i < k; i++)
msgWindow.document.write(" " + value[i]);
msgWindow.document.writeln("");
}
// unhide -->
</SCRIPT>
<style type="text/css">
<!--
.style2 {color: #FFFFFF}
-->
</style>
</head>
<body bgcolor="white" text="black">
<p><strong>PERMUTATION LISTING GENERATER </strong></p>
<p>Make sure the device has enough <i>Primary Memory</i> to allocate. It can hang the system while running full function, not an infinite process. </p>
<p> </p>
<LEFT>
<FORM name=calc>
<table border=1 bordercolor=darkblue cellspacing=0>
<tr><td>
<table>
<tr><td><b>Number<span class="style2"> - n</span> </b>
<td><INPUT maxLength=20 name=number>
<tr><td><b>Select<span class="style2"> - k </span></b>
<td><input maxlength=20 name=select>
<tr><td><b>Permutations </b>
<td><INPUT maxLength=20 readonly name=permutations>
</table>
</tr>
</table>
</FORM>
<INPUT onclick=listing(); type=button value=Listing>
</LEFT>
</html>
此处排列列表不遵循任何顺序。就像这样:
5P3 = 60
列表的列表
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 4
1 4 2
等。我需要按升序排列这些数字。请帮我。