static void Main(string[] args)
{
//n - quantity of rows, m - quantity of columns
// p,q - references to current element of The Matrix A[][]
// p=1, q=3 ----> A[0][3] - the element of crossing first row and fourth column
int p = 0;
int q = 0;
int j = 0;
int a = 0;
int b = 0;
int bentCounter = 0;
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
int n = a;
int m = b;
int mn = m * n;
int [,] A = new int [a,b];
int[] B = new int[] { 0, 1, 0, -1 };
int[] C = new int[] { 1, 0, -1, 0 };
for (int i = 0; i < mn ; i++)
{
bentCounter++;
if (bentCounter == n) {j++;}
if (bentCounter == m + n - 1)
{
if (j == 3) { j = -1; }
j++;
bentCounter = 0;
n--; m--;
}
A [p,q] = i;
p += B[j];
q += C[j];
}
for (int r = 0; r < A.GetLength(0); r++)
{
for (int c = 0; c < A.GetLength(1); c++)
{
Console.Write(" " + A[r, c] + " ");
}
Console.WriteLine();
}
10x感谢您的帮助 BR
答案 0 :(得分:1)
你以错误的方式递增维度并超出界限,快速解决方案是将它们换成圆形,然后将int [,] A = new int [a,b]
更改为
int [,] A = new int [b,a];
一切都很好;)
编辑:同时更改此行以填充输出会为您提供一个漂亮的方阵
Console.Write(" " + A[r, c].ToString().PadLeft(mn.ToString().Length, ' '));
答案 1 :(得分:1)
您还可以更改以下内容
if (bentCounter == n) { j++; }
到
if (bentCounter == m) { j++; }
它应该可以正常工作。原因是你的B和C前缀以这种方式组织。
答案 2 :(得分:1)
答案 3 :(得分:0)
试一试:它是一个100%动态螺旋矩阵供用户使用。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>Total Row Column :</td>
<td>
<input type="text" runat="server" id="txtbox" style="width: 100px;" /></td>
</tr>
<tr>
<td >Starting No :</td>
<td>
<input type="text" runat="server" id="txtstartwith" style="width: 100px;" /></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<asp:Button ID="Button1" runat="server" Text="Generate Spiral Matrix" OnClick="Button1_Click" /></td>
</tr>
</table>
<div id="divsp" runat="server">
</div>
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
divsp.InnerHtml = "";
int ignoreMe;
bool successfullyParsed = int.TryParse(txtbox.Value, out ignoreMe);
if (successfullyParsed)
{
successfullyParsed = int.TryParse(txtstartwith.Value, out ignoreMe);
if (successfullyParsed)
{
// ...
}
else
{
txtstartwith.Value = "0";
}
}
else
{
txtbox.Value = "0";
}
if (txtbox.Value != "" && txtbox.Value != "0" && Convert.ToInt32(txtbox.Value) > 0 && txtstartwith.Value != "")
{
int N = Convert.ToInt32(txtbox.Value);
int row = N;
int column = N;
int[,] A = new int[N, N];
int ele = N * N;
ele = ele + (Convert.ToInt32(txtstartwith.Value) - 1);
int no = Convert.ToInt32(txtstartwith.Value);
int a = 0;
int b = 0;
int bo = 0;
int le = 0;
int loopbreak = 0;
while (no <= ele)
{
for (; b < N; b++)
{
A[a, b] = no;
if (no == ele)
{
loopbreak = 1;
break;
}
else
{
no++;
}
}
if (loopbreak == 1)
{
break;
}
if (b == N)
{
b = N - 1;
for (; a < b; a++)
{
int i = a + 1;
A[i, b] = no;
if (no == ele)
{
loopbreak = 1;
break;
}
else
{
no++;
}
}
}
if (loopbreak == 1)
{
break;
}
if (a == N - 1)
{
for (; b > le; b--)
{
int i = b - 1;
A[a, i] = no;
if (no == ele)
{
loopbreak = 1;
break;
}
else
{
no++;
}
}
}
if (loopbreak == 1)
{
break;
}
if (a == N - 1 && b == bo)
{
for (; a > le + 1; a--)
{
int i = a - 1;
A[i, b] = no;
if (no == ele)
{
loopbreak = 1;
break;
}
else
{
no++;
}
}
b = b + 1;
}
N = N - 1;
bo = bo + 1;
le = le + 1;
}
StringBuilder str = new StringBuilder();
str.Append("<table>");
for (int m = 0; m < row; m++)
{
str.Append("<tr>");
for (int k = 0; k < row; k++)
{
str.Append("<td style='padding:5px;text-align:center;'>" + A[m, k].ToString() + "</td>");
}
str.Append("</tr>");
}
str.Append("</table>");
divsp.InnerHtml = str.ToString().Trim();
}
}