随机排列n*n方阵

将n组1-n随机置于n*n的单元格中,使得每行,每列都是1-n的一个全排列

 

  1. Sub Perm()
  2. Dim n As Long, i As Long, j As Long, arr(), brr(), t As Long
  3. n = 20
  4. ReDim arr(n - 1, n - 1)
  5. ReDim brr(n - 1)
  6. For i = 0 To n ^ 2 - 1
  7. arr(i \ n, i Mod n) = (i \ n + i) Mod n + 1
  8. Next
  9. Randomize
  10. For i = n - 1 To 0 Step -1
  11. t = Int(Rnd * i + 1)
  12. For j = 0 To n - 1
  13. brr(j) = arr(j, t)
  14. Next
  15. For j = 0 To n - 1
  16. arr(j, t) = arr(j, i)
  17. arr(j, i) = brr(j)
  18. Next
  19. t = Int(Rnd * i + 1)
  20. For j = 0 To n - 1
  21. brr(j) = arr(t, j)
  22. Next
  23. For j = 0 To n - 1
  24. arr(t, j) = arr(i, j)
  25. arr(i, j) = brr(j)
  26. Next
  27. Next
  28. [a1].Resize(n, n) = arr
  29. End Sub

执行结果:

 

1191468918101611134201735152712
3116810112012181315621957174914
1083151718719520213961214411161
1614913413511681915121820101727
1412719121139461713101618815205
2201579101911171214511846163813
1715102451461279201613191111838
1513820231241057181411171991616
2018135781791510123191624141611
1816113561571381011714202121949
1311618201102835161291517714194
7520121415416
这个任务可以分为三个部分来完成: ### 初始化 1. 定义n和m变量,使用Random类生成[1, n]范围内的随机数字,并存储在数组中。 2. 对数组进行升序排序,并显示图形。 C#代码示例: ```csharp int n = new Random().Next(5, 21); // 生成[5, 20]范围内的随机数字 int[] numbers = new int[n]; for (int i = 0; i < n; i++) { numbers[i] = new Random().Next(1, n + 1); // 生成[1, n]范围内的随机数字 } Array.Sort(numbers); // 升序排序 for (int i = 0; i < n; i++) { Console.Write(numbers[i] + " "); } ``` ### 专注力训练 1. 定义m变量,使用Random类生成[5, 20]范围内的随机数字。 2. 使用PictureBox控件显示m * m个随机图片,并在下方设置一个文本框。 3. 点击开始按钮启动计时器,并记录开始时间。 4. 用户输入数字并检验与图片对应数字是否相同,计算正确率。 5. 点击结束按钮停止计时器,并记录结束时间和正确率。 C#代码示例: ```csharp int m = new Random().Next(5, 21); // 生成[5, 20]范围内的随机数字 PictureBox[,] pictureBoxes = new PictureBox[m, m]; TextBox[,] textBoxes = new TextBox[m, m]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { pictureBoxes[i, j] = new PictureBox(); pictureBoxes[i, j].Image = GetRandomImage(); // 获取随机图片 pictureBoxes[i, j].SizeMode = PictureBoxSizeMode.StretchImage; pictureBoxes[i, j].Location = new Point(i * 50, j * 50); pictureBoxes[i, j].Size = new Size(50, 50); this.Controls.Add(pictureBoxes[i, j]); textBoxes[i, j] = new TextBox(); textBoxes[i, j].Location = new Point(i * 50, j * 50 + 50); textBoxes[i, j].Size = new Size(50, 20); this.Controls.Add(textBoxes[i, j]); } } DateTime startTime = DateTime.Now; Timer timer = new Timer(); timer.Interval = 1000; timer.Tick += Timer_Tick; timer.Start(); double correctCount = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { int number = GetImageNumber(pictureBoxes[i, j].Image); // 获取图片对应数字 int inputNumber; if (int.TryParse(textBoxes[i, j].Text, out inputNumber)) { if (number == inputNumber) { correctCount++; } } } } double accuracy = correctCount / (m * m); void Timer_Tick(object sender, EventArgs e) { TimeSpan timeSpan = DateTime.Now - startTime; Console.WriteLine("用时:" + timeSpan.TotalSeconds + "秒"); Console.WriteLine("正确率:" + accuracy); timer.Stop(); } ``` ### 历史记录 1. 使用SQLite或其他数据库存储每次训练的结果,包括n、m、用时和正确率。 2. 查询不同n和m时,返回历史最好成绩。 3. 查询某一n和某一m时,返回所有的历史成绩。 C#代码示例: ```csharp // 初始化数据库连接 string connectionString = "Data Source=training.db"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); // 创建表 using (SQLiteCommand command = new SQLiteCommand("CREATE TABLE IF NOT EXISTS Training (Id INTEGER PRIMARY KEY AUTOINCREMENT, N INTEGER, M INTEGER, Time REAL, Accuracy REAL)", connection)) { command.ExecuteNonQuery(); } // 插入数据 using (SQLiteCommand command = new SQLiteCommand("INSERT INTO Training (N, M, Time, Accuracy) VALUES (@N, @M, @Time, @Accuracy)", connection)) { command.Parameters.AddWithValue("@N", n); command.Parameters.AddWithValue("@M", m); command.Parameters.AddWithValue("@Time", timeSpan.TotalSeconds); command.Parameters.AddWithValue("@Accuracy", accuracy); command.ExecuteNonQuery(); } // 查询不同n和m的历史最好成绩 using (SQLiteCommand command = new SQLiteCommand("SELECT N, M, MIN(Time) AS BestTime, MAX(Accuracy) AS BestAccuracy FROM Training GROUP BY N, M", connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { int n = reader.GetInt32(0); int m = reader.GetInt32(1); double bestTime = reader.GetDouble(2); double bestAccuracy = reader.GetDouble(3); Console.WriteLine("n={0}, m={1}, 历史最好成绩:用时={2}秒,正确率={3}", n, m, bestTime, bestAccuracy); } } } // 查询某一n和某一m的所有历史成绩 using (SQLiteCommand command = new SQLiteCommand("SELECT Time, Accuracy FROM Training WHERE N=@N AND M=@M", connection)) { command.Parameters.AddWithValue("@N", 10); command.Parameters.AddWithValue("@M", 10); using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { double time = reader.GetDouble(0); double accuracy = reader.GetDouble(1); Console.WriteLine("用时={0}秒,正确率={1}", time, accuracy); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值