C++:以对角线方式处理2d数组元素

C++: Process 2d array elements in a diagonal fashion

本文关键字:处理 2d 数组元素 方式 对角线 C++      更新时间:2023-10-16

假设我们有一个二维数组arr[N][N],其中Nconstant 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(因此它们可以用作索引)。我们制作了两个副本,tmpx

现在我们从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 ..