使用 C++ 中的矩阵计算所有排序行数
在本教程中,我们将讨论一个程序,此程序用于查找矩阵中所有已排序行数。
为此,我们需提供 m*n 矩阵。我们的任务是计算给定矩阵中按升序或降序排序的所有行。
示例
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
//counting sorted rows
int count_srows(int mat[][MAX], int r, int c){
int result = 0;
for (int i=0; i<r; i++){
int j;
for (j=0; j<c-1; j++)
if (mat[i][j+1] <= mat[i][j])
break;
if (j == c-1)
result++;
}
for (int i=0; i<r; i++){
int j;
for (j=c-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 = 4, n = 5;
int mat[][MAX] = {{1, 2, 3, 4, 5}, { 4, 3, 1, 2, 6}, {8, 7, 6, 5, 4}, {5, 7, 8, 9, 10}};
cout << count_srows(mat, m, n);
return 0;
}输出
3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP