按對角線模式列印矩陣
給定一個 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
Step 5 -> Loop For i=0 and i<r and i++ Print mat[i][4-1-i] End STOP
示例
#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 16 4 7 10 13
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP