110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var 行数 = 50;
var array = new int[行数, 10];
随机产生数字(array);
var query1 = from row in Enumerable.Range(0, array.GetLength(0) - 4)
from col in Enumerable.Range(0, 10)
where array[row, col] == array[row + 3, col] && array[row + 1, col] == array[row + 4, col]
select new { row, col };
int cnt = 0;
Console.WriteLine("\r\n前后对称的第一个数字位置:");
foreach (var x in query1)
Console.Write("{0}: {1}行 {2}列\r\n", ++cnt, x.row + 1, x.col + 1);
Console.WriteLine();
cnt = 0;
var query2 = from row in Enumerable.Range(0, array.GetLength(0) - 4)
from col in Enumerable.Range(0, 10)
where array[row + 1, col] == array[row + 3, col] && array[row + 2, col] == array[row + 4, col]
let 第五个数字是否符合 = array[row, col] == array[row + 4, col] || array[row + 1, col] == array[row + 4, col]
select new { row, col, 第五个数字是否符合 };
var lst2 = query2.ToList();
Console.WriteLine("\r\n数字引并且第5个数字符合:");
foreach (var x in query2.Where(x => x.第五个数字是否符合))
Console.Write("{0}: {1}行 {2}列\r\n", ++cnt, x.row + 1, x.col + 1);
cnt = 0;
Console.WriteLine("\r\n数字引并且第5个数字不符合:");
foreach (var x in query2.Where(x => !x.第五个数字是否符合))
Console.Write("{0}: {1}行 {2}列\r\n", ++cnt, x.row + 1, x.col + 1);
Console.ReadKey();
}
private static Random Rnd = new Random();
private static void 随机产生数字(int[,] array)
{
for (var i = 0; i < array.GetLength(0); i++)
{
Console.Write("{0:D2}行: ", i + 1);
var lst = Enumerable.Range(1, 10).Select(x => Rnd.Next(10) + 1).OrderBy(x => Rnd.Next()).ToArray();
for (var j = 0; j < 10; j++)
{
Console.Write("{0:D2} ", lst[j]);
array[i, j] = lst[j];
}
Console.WriteLine();
}
}
}
}