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