给定一个 n*n 的二维数组,任务是找到给定矩阵的反螺旋排列
input : arr[4][4]={1,2,3,4, 5,6,7,8, 9,10,11,12 13,14,15,16}output : 1 6 11 16 4 7 10 13
算法startstep 1 -> declare start variables as r=4, c=4, i and jstep 2 -> initialize array as mat[r][c] with elementsstep 3 -> loop for i=0 and i<r and i++ print mat[i][j]step 4 -> print
step 5 -> loop for i=0 and i<r and i++ print mat[i][4-1-i]endstop
example的中文翻译为:示例#include<iostream>#include <bits/stdc++.h>using namespace std;int main() { int r=4,c=4,i,j; int mat[r][c] = { {1,2,3, 4}, {5,6,7,8},{9,10,11,12},{13,14,15,16}}; for(i=0;i<r;i++) { cout<<mat[i][i]<<" "; } cout<<"
"; for(i=0;i<r;i++) { cout<<mat[i][4-1-i]<<" "; }}
输出如果我们运行上面的程序,它将生成以下输出
1 6 11 164 7 10 13
以上就是打印矩阵的对角线模式的详细内容。