C++:以对角线方式处理2d数组元素
C++: Process 2d array elements in a diagonal fashion
假设我们有一个二维数组arr[N][N]
,其中N
是constant integer
。假设arr
的每个元素都被初始化。
如何使用嵌套for循环打印arr
反诊断元素?
我的意思是:
- 在最外层循环的第一次迭代后,将打印
arr[0][0]
- 在最外循环的第二次迭代后,将打印
arr[0][1]
和arr[1][0]
- 在最外循环的第三次迭代后,将打印
arr[0][2]
、arr[1][1]
和arr[2][0]
- 在最外层循环的最后一次迭代之后,将打印
arr[N-1][N-1]
谢谢你抽出时间!
对不起所有写下"下半场应该相似"的人。。。事实并非如此。
不管怎样,给你:
// traverse array diagonally
int c, tmp, x;
for (c = N - 1; c > -N; c--) {
tmp = N - abs(c) - 1;
x = tmp;
while (x >= 0) {
if (c >= 0) {
std::cout << arr[x][tmp - x] << ", ";
}
else {
std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", ";
}
--x;
}
std::cout << "n";
}
你需要这个玩游戏吗?
再看一遍,我觉得我的答案写得不太好。下面是一个快速浏览:
让我们假设N是3。
我们需要的是对坐标组合的迭代,如下所示:
(0, 0)
(1, 0), (0, 1)
(2, 0), (1, 1), (0, 2)
(2, 1), (1, 2)
(2, 2)
因此,首先是一些占位符:
int c, // a counter, set by the outer loop
tmp, // for intermediate results
x; // the x-index into *arr* (*y* will be defined implicitly)
现在这个外环
for (c = N - 1; c > -N; c--) {
使c迭代{2,1,0,-1,2}。
下一步
tmp = N - abs(c) - 1;
x = tmp;
将{2,1,0,-1,-2}转换为{0,1,2,1},这是该步骤中所需输出的长度减去1(因此它们可以用作索引)。我们制作了两个副本,tmp和x。
现在我们从x倒计时到0:
while (x >= 0) {
...
--x;
}
如果我们在arr的左上半部分,由c>=0表示,rr中的x索引需要从对角线开始并向下到零(0到0,1到0和2到0),而y索引需要从零开始并向上到对角线
if (c >= 0) {
std::cout << arr[x][tmp - x] << ", ";
}
一旦我们在右下半部分,x指数需要从N开始,向下到对角线(2比1和2比2),而y指数需要从对角线开始,向上到N(1比2和2比1):
else {
std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", ";
}
最后,我们只需要在每行的末尾进行换行:
std::cout << "n";
精明?:-)
这将适用于一半的矩阵。。另一半将类似:
for (j = 0 ; j < N ; j++)
{
for (i = 0 ; i <= j ; i ++)
{
printf("%d n",a[i,j-i]);
}
}
您可以注意到,对于任何对角线,2个"相邻"元素由[x][y]
和[x+1][y-1]
给出:也就是说,您向右和向上迈出对角线一步。
所以你可以有一个循环,设置对角线的第一个单元格。您只需要从[0][y]
开始迭代y
的所有值,然后向上(对角)执行此步骤,直到到达顶部或右侧。然后,你也需要从[0][N-1]
移动到[N-1][N-1]
来覆盖下半场。
代码如下:
for (int _y = 0; _y < N; _y++) {
int x = 0, y = _y;
while (x < N && y >= 0) {
cout << arr[x][y];
x++; y--;
}
cout << endl; // don't forget a newline
}
我将省略代码的后半部分,因为它应该大致相同。
这是一段java代码,但算法是相同的
for(int i = 0; i < 10; i++){
for(int j = 0; j <= i; j++){
System.out.print(a[j][i-j] + " ");
}
System.out.println();
}
看起来像这样:
for(row = 0; row < N; row++){
for(j = 0; j <= row; j++){
print Array[row - j][j];
}
newline;
}
以下是矩阵两半的解决方案:
//First half (including middle diagonal)
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
print array[j][i - j];
}
newline;
}
//Second half (excluding middle diagonal)
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
print array[n - i + j][n - j - 1];
}
newline;
}
这里有一个我认为有用的解决方案R是总行数。
void diagonalOrder(int arr[][COLS],int R)
{
for (int i = 0; i < R+COLS-1; i++)
{
int col;
int row;
i<COLS?col=i:col=(COLS-1);
col>i?row=col-i:row=i-col;
for(int j=col;j>=0 ;j--)
{
if(row<R)
cout<<arr[row][j]<<" ";
row++;
}
cout<<endl;
}
}
ie.
const int ROWS = 4;
const int COLS = 3;
int arr[][COLS] = {{ 1, 2, 4 },
{ 3, 5, 7},
{ 6, 8, 10},
{ 9, 11, 12}
};
diagonalOrder(arr,ROWS);
Output
----------
1
2 3
4 5 6
7 8 9
10 11
12
------------------------------------------------------
const int ROWS = 8;
const int COLS = 3;
int arr8[][COLS] = {{ 1, 2, 4 },
{ 3, 5, 7 },
{ 6, 8, 10 },
{ 9, 11, 13 },
{ 12, 14, 16},
{ 15 ,17, 19},
{ 18 ,20, 22},
{ 21, 23, 24}
};
cout<<"nn8*3 Matrix"<<endl<<endl;
diagonalOrder(arr8,8);
--------------------------------------------------------------
Output
--------------------------------------------------------------
1
2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23
24
-----------------------------------------
int arr[][COLS] = {{ 1, 2, 4 ,20},
{ 3, 5, 7,20},
{ 6, 8, 10,20},
{ 9, 11, 12,20}
};
-------------------------------------------------------------
Output
-------------------------------------------------------------
1
2 3
4 5 6
20 7 8 9
20 10 11
20 12
20
You can work with n*n Matrix ..
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 2D数组来自文本输入,中间有空格
- 处理多个异常集合的C++方法
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 如何处理以 2D 数组编写的条件
- 处理2D数组时的分割故障
- C++图像处理 - 将图像文件读入 2D 数组
- 在 for 循环中创建多个线程以处理 2D 数组
- 使用形状参数时,如何灵活地使用可以处理 2D 和 3D 形状的方法参数
- 如何处理 2D opengl 项目的纹理
- 设置碰撞检测,然后在 2D 游戏中处理碰撞
- 在c++中进行线程处理,在2d数组中找到最大的条目
- 未处理的异常访问违反2D DirectX渲染D2D1.dll
- 2D数组和文件处理[C++]
- 寻找一种可以替代python的处理速度:2D量子粒子
- 哪种数据结构可以处理2d线段
- C++:以对角线方式处理2d数组元素