卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18142本站已运行340

打印矩阵的对角线模式

给定一个 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

打印矩阵的对角线模式

算法

'
START
Step 1 -> declare start variables as r=4, c=4, i and j
Step 2 -> initialize array as mat[r][c] with elements
Step 3 -> Loop For i=0 and i<r and i++
   Print mat[i][j]
Step 4 -> print <p>
Step 5 -> Loop For i=0 and i<r and i++
   Print mat[i][4-1-i]
End
STOP</p>

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<<"<p>";
   for(i=0;i<R;i++) {
      cout<<mat[i][4-1-i]<<" ";
   }
}</p>

输出

如果我们运行上面的程序,它将生成以下输出

'
1 6 11 16
4 7 10 13
卓越飞翔博客
上一篇: 在PHP中的fread()函数
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏