我保证是一个完美的方阵.我想在这种情况下从矩阵的中心开始matrix[2][2],我知道如何计算中心(int)(dimensions / 2).我需要以下面的向外螺旋模式输出数组的内容.当然,算法应该适用于任何完美的方阵.我不确定这个算法是否已经存在,我不想重新发明轮子.
int dimensions / 2;
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
Run Code Online (Sandbox Code Playgroud)
这个例子的输出应该是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Run Code Online (Sandbox Code Playgroud) 我有一个我自己的strcmp版本,看起来像这样
int strcmp(char str1[], char str2[])
{
int i = 0;
while ((str1[i] == str2[i]) && (str1[i] != '\0'))
{
i++;
}
if (str1[i] > str2[i])
return 1;
if (str1[i] < str2[i])
return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的测试用例是
char a[20];
char b[20];
b[0] = 'f';
a[0] = 'f';
cout << strcmp(b, a) << endl;
Run Code Online (Sandbox Code Playgroud)
但是,我输出1,意味着它们彼此不相等.如果我在函数调用中交换a和b的位置,我得-1.当我的char都是'f'时,我不确定为什么我无法在比较中得到0返回.我觉得这是如此基本,我不知道为什么我的比较是关闭的
str1[i] > str2[i]
Run Code Online (Sandbox Code Playgroud) 我有这个非常大的数组,称为网格.当我按如下方式声明数组时,根据整数的数组构造函数,数组中的每个值都应设置为0
int testGrid[226][118];
Run Code Online (Sandbox Code Playgroud)
然而,当我遍历整个数组时,我似乎为数组的大部分得到0,但是在数组的下半部分,我得到任意垃圾.似乎解决方案是迭代数组并手动将每个值设置为0.有更好的方法吗?