Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:以对角方式处理二维阵列元素_C++_Arrays_Loops_Nested_2d - Fatal编程技术网

C++ C++;:以对角方式处理二维阵列元素

C++ C++;:以对角方式处理二维阵列元素,c++,arrays,loops,nested,2d,C++,Arrays,Loops,Nested,2d,假设我们有一个二维数组,arr[N][N],其中N是一个常量整数。 假设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]将被打印 谢谢你的时间 这将适用于矩阵的一半。。另一

假设我们有一个二维数组,
arr[N][N]
,其中
N
是一个
常量整数。
假设
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]
    将被打印

谢谢你的时间

这将适用于矩阵的一半。。另一半类似:

for (j = 0 ; j < N ; j++)
{
   for (i = 0 ; i <= j ; i ++)
   {
      printf("%d \n",a[i,j-i]);
   }
}
(j=0;j {
对于(i=0;i,这里是一段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(int i=0;i<10;i++){

对于(int j=0;j您可以注意到,对于任何对角线,由
[x][y]
[x+1][y-1]
给出2个“相邻”元素:也就是说,您向右和向上走对角线

因此,您可以使用一个循环来设置对角线的第一个单元格。您只需要从
[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
}
(int _y=0;_y{ int x=0,y=_y; 而(x=0){
cout看起来像这样:

for(row = 0; row < N; row++){  
   for(j = 0; j <= row; j++){  
      print Array[row - j][j];  
   }  
   newline;  
}  
(行=0;行对于(j=0;j对于所有写下“下半场应该是相似的”的人,我感到抱歉……事实并非如此

不管怎样,给你:

// 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";
}
因此,首先是一些占位符:

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,0},这是这一步所需输出的长度减去1(因此它们可以用作索引)。我们制作了两个副本,tmp和x

现在我们从x倒数到0:

如果我们位于arr的左上半部分,由c>=0表示,则进入arr的x索引需要从对角线开始并向下到零(0到0、1到0和2到0),而y索引需要从零开始并向上到对角线(0到0、0到1和0到2):

如果(c>=0){

std::cout这是矩阵两半的解决方案:

    //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;
    }
//前半部分(包括中间对角线)
对于(int i=0;i
我认为有一个解决方案很有帮助 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<<"\n\n8*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 ..
void对角线顺序(int-arr[][COLS],int-R)
{
对于(int i=0;i=0;j--)
{

如果(rowlol,这个问题的措辞确实有点像硬件问题,但实际上不是;)JPEG的Z字形遍历?+1进行解释,尽管我更喜欢其他答案中的代码谢谢你给我一个完整的答案;)。我之所以需要它,是因为存在一个嵌套的for循环,由于数据依赖性,它无法按行或列并行,因此只能按反对角线方式进行。好吗?听起来很可怕:-)好吧,如果它对您有效,请单击“接受”好吗?谢谢!!:-)
        if (c >= 0) {
            std::cout << arr[x][tmp - x] << ", ";
        }
        else {
            std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", ";
        }
    std::cout << "\n";
    //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;
    }
    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<<"\n\n8*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 ..