C++ 中計算矩陣中降序排列的所有列
本教程中,我們將討論一個程式,用於查詢矩陣中排序為降序的列數。
為此,我們將得到一個矩陣。我們的任務是計算矩陣中元素按降序排列的列數。
示例
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
//counting columns sorted in descending order
int count_dcolumns(int mat[][MAX], int r, int c){
int result = 0;
for (int i=0; i<c; i++){
int j;
for (j=r-1; j>0; j--)
if (mat[i][j-1] >= mat[i][j])
break;
if (c > 1 && j == 0)
result++;
}
return result;
}
int main(){
int m = 2, n = 2;
int mat[][MAX] = {{1, 3}, {0, 2,}};
cout << count_dcolumns(mat, m, n);
return 0;
}輸出
2
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP