题目是:若干个不重复数,打乱顺序输出,用javascript实现
实现如下:
window.onload = function () {
// 1到100,打乱顺序输出
outputOne();
document.write("<br /><br />");
// 自定义的列表,打乱顺序输出
outputTwo();
};
function outputOne() {
var count = 100;
var size = 10;
for (var i = 1; i <= count; i++) {
document.write(i + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
var list = [];
var number;
var listCount;
while (list.length < count) {
listCount = 0;
number = Math.floor(Math.random() * count) + 1;
for (var i = 0; i < list.length; i++) {
if (number != list[i]) {
listCount++;
};
};
if (listCount == list.length) {
list.push(number);
};
};
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};
function outputTwo() {
var size = 10;
var list = [1, 2, 3, 4, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000, 6, 7, 8, 9];
var newList = [];
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
var index;
var listCount;
while (newList.length < list.length) {
listCount = 0;
index = Math.floor(Math.random() * list.length);
for (var i = 0; i < newList.length; i++) {
if (list[index] != newList[i]) {
listCount++;
};
};
if (listCount == newList.length) {
newList.push(list[index]);
};
};
for (var i = 1; i <= newList.length; i++) {
document.write(newList[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};
// 1到100,打乱顺序输出
outputOne();
document.write("<br /><br />");
// 自定义的列表,打乱顺序输出
outputTwo();
};
function outputOne() {
var count = 100;
var size = 10;
for (var i = 1; i <= count; i++) {
document.write(i + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
var list = [];
var number;
var listCount;
while (list.length < count) {
listCount = 0;
number = Math.floor(Math.random() * count) + 1;
for (var i = 0; i < list.length; i++) {
if (number != list[i]) {
listCount++;
};
};
if (listCount == list.length) {
list.push(number);
};
};
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};
function outputTwo() {
var size = 10;
var list = [1, 2, 3, 4, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000, 6, 7, 8, 9];
var newList = [];
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
var index;
var listCount;
while (newList.length < list.length) {
listCount = 0;
index = Math.floor(Math.random() * list.length);
for (var i = 0; i < newList.length; i++) {
if (list[index] != newList[i]) {
listCount++;
};
};
if (listCount == newList.length) {
newList.push(list[index]);
};
};
for (var i = 1; i <= newList.length; i++) {
document.write(newList[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};
感谢 明天.Net(小彭) 同学提供一种更高明的思路:
利用往新的数组中随意位置依次插入新的元素的方式得到一个随机排列,即我们需要的结果
function outputThree() {
var size = 10;
var list = [1, 2, 3, 4, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000, 6, 7, 8, 9];
var outlist = [];
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
for (var i = 0; i < list.length; i++) {
if (i == 0) {
outlist.push(list[i]);
} else {
var index = Math.floor(Math.random() * (outlist.length + 1));
outlist.splice(index, 0, list[i]);
};
};
for (var i = 1; i <= outlist.length; i++) {
document.write(outlist[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};
var size = 10;
var list = [1, 2, 3, 4, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000, 6, 7, 8, 9];
var outlist = [];
for (var i = 1; i <= list.length; i++) {
document.write(list[i - 1] + " ");
if (i > 1 & i % size == 0) {
document.write("<br />");
};
};
document.write("<br /><br />");
for (var i = 0; i < list.length; i++) {
if (i == 0) {
outlist.push(list[i]);
} else {
var index = Math.floor(Math.random() * (outlist.length + 1));
outlist.splice(index, 0, list[i]);
};
};
for (var i = 1; i <= outlist.length; i++) {
document.write(outlist[i - 1] + " ");
if (i > 1 && i % size == 0) {
document.write("<br />");
};
};
};