这听起来有点像家庭作业。这里的洗牌整数的固定范围(在你的情况下,10×10)到二维整数数组,如int[10,10]
的想法:
using System;
using System.Linq;
using System.Collections;
class MainClass {
public static void Main (string[] args) {
// table dimension (assumes a square)
var dim = 10;
var table = new int?[dim, dim];
// 100 integers: 0..99
var queue = new Queue(Enumerable.Range(0, dim * dim).ToList<int>());
var rng = new Random();
int x = dim/2, y = dim/2;
// Acceptable shuffle? As long as the queue has anything in it, try to place the next number
while(queue.Count > 0) {
x = rng.Next(dim); // still using random, not great! :(
y = rng.Next(dim);
if(table[x,y] == null)
table[x,y] = (int)queue.Dequeue();
}
// print output so I know I'm not crazy
for(var i = 0; i < dim; i++) {
Console.Write("Row {0}: [", i);
for(var j = 0; j < dim; j++) {
Console.Write("{0,4}", table[i,j]);
}
Console.WriteLine("]");
}
}
}
输出:
Mono C# compiler version 4.0.4.0
Row 0: [ 55 45 38 23 88 46 7 89 0 94]
Row 1: [ 2 92 43 51 58 67 82 90 79 17]
Row 2: [ 29 64 16 8 50 14 1 25 26 73]
Row 3: [ 97 37 13 20 4 75 98 80 48 12]
Row 4: [ 33 27 42 74 95 35 57 53 96 60]
Row 5: [ 59 86 76 40 6 11 77 49 93 61]
Row 6: [ 5 72 9 91 68 30 39 69 99 21]
Row 7: [ 52 31 28 34 3 81 18 62 10 71]
Row 8: [ 66 24 44 54 56 85 84 22 47 63]
Row 9: [ 65 36 83 41 15 19 87 78 70 32]