- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,我的代码正在填充螺旋矩阵。当 rows=columns 时它工作得很好但是当不同时它会在螺旋的第一个弯曲处给出错误!!!我试过用断点调试它,但没有发现任何错误!所以关于我的代码的一些额外提示:bentCounter 查找行或列何时被填充,如果是,它通过递增 j 来旋转螺旋线。当 j++ 使用数组 B 和 C 的下一个元素时,A 数组的索引 p、q 的方向就会改变!当我们同时填充行和列时,留下一个子矩阵,所以 n--;米--;当 j=3 时,它应该被取消以开始一个新的漩涡。希望已经足够清楚了!
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();
}
10 倍感谢您的帮助BR
最佳答案
你以错误的方式增加维度并超出界限,一个快速的解决方案是将它们交换,所以将 int [,] A = new int [a,b]
更改为
int [,] A = new int [b,a];
一切都很好;)
编辑:同时更改此行以填充输出为您提供一个漂亮的方阵
Console.Write(" " + A[r, c].ToString().PadLeft(mn.ToString().Length, ' '));
关于c# - 螺旋矩阵算法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4431780/
我正在尝试创建一个结构,如下面的屏幕截图所示。有没有办法在 JavaScript 中为此构建一个算法,以按时间顺序获取每个红点的 X 和 Y 坐标,以根据特定数量生成无限螺旋? Screenshot
我在 C++ 中有如下代码: for(int x = position.x; x GetValue(tc); } } 它的作用是生成一个梯度噪声值容器。在这样做的同时,它还会使结果围绕震中螺旋
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!