作业帮 > 综合 > 作业

如何使用C# 语言实现螺旋加上蛇形的动态生成矩阵?n随机取数.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/15 18:10:37
如何使用C# 语言实现螺旋加上蛇形的动态生成矩阵?n随机取数.
例如当n=4 ,矩阵是这样的 1 2 3 4 11 9 10 5 1512 8 6 16 14 13 7
其中螺旋部分是
1 2 3 4
9 10 5
8 6
7
蛇形是这样的
11
1512
1614 13
整个矩阵
1 2 3 4
11 9 10 5
15 12 8 6
16 14 13 7
紧急紧急!
static void Main(string[] args)
{
int n = 2;
int count = 0;
List sz = new List { 1,2,3,4,11,9,10,5,15,12,8,6,16,14,13,7 };
Dictionary l = new Dictionary();
Dictionary s = new Dictionary();
List tempLst;
do
{
tempLst = new List();
for (int i = 0; i < n; i++)
{
if (sz.Count >= n)
{
tempLst.Add(sz[i]);
}
}
l.Add(count,tempLst);
if (n > 0)
{
if (sz.Count >= n)
{
sz.RemoveRange(0,n);
}
else
{
sz.Clear();
}
}
n--;
count++;
tempLst = new List();
for (int i = 0; i < count; i++)
{
if (sz.Count >= count)
{
tempLst.Add(sz[i]);
}
}
s.Add(count - 1,tempLst);
if (count > 0)
{
if (sz.Count >= count)
{
sz.RemoveRange(0,count);
}
else
{
sz.Clear();
}
}
} while (sz.Count > 0);
foreach (var key in l.Keys)
{
foreach (var item in l[key])
{
Console.Write(item + "\t");
}
Console.Write("\n");
}
foreach (var key in s.Keys)
{
foreach (var item in s[key])
{
Console.Write(item + "\t");
}
Console.Write("\n");
}
}
动手写了下,这个是你需要的么?