enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testing_random
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int[,] a = new int[n,n];//declaring the matrix
Random o = new Random();
a[0,0] = o.Next(n);
for (int i = 1; i < n; i++)//filling the first line
{
int d = 1;
while (d != 0)
{
a[i,0] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i,0] == a[j,0])
d++;
}
}
for (int i = 1; i < n; i++)//filing the first column
{
int d = 1;
while (d != 0)
{
a[0, i] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[0, i] == a[0, j])
d++;
}
}
for (int k = 1; k < n; k++)//filling the rest of the matrix
for (int i = 1; i < n; i++)
{
int d = 1;
while (d != 0)
{
a[i, k] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i, k] == a[j, k])
d++;
for (int j = 0; j < k; j++)
if (a[i, k] == a[i, j])
d++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0} ", a[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
輸出應該是4 * 4的矩陣,其中每列和每行包含每個數字一次。 問題是當我運行代碼時,不是每次我得到輸出時,我認爲問題不是每一組第一行和列都可以根據需要給出矩陣,我進入了一個無止境的循環。 我想要做的就是將應用程序的運行時間限制爲每個示例100毫秒,所以如果未填充矩陣,程序將重新啓動 我缺少哪些代碼?我正在製作一個數獨遊戲#
我想這會工作。謝謝 – Elie 2015-02-08 18:48:15
請讓我們知道它是否工作。一旦有效,請「接受」對您最有幫助的答案。 – 2015-02-08 19:08:56