计算C ++矩阵中所有排序的行
在本教程中,我们将讨论一个程序,以查找矩阵中所有已排序行的数量。
为此,我们将提供m*n矩阵。我们的任务是计算给定矩阵中按升序或降序排序的所有行。
示例
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
//计算排序的行
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
热门推荐
8 火把节祝福语简短
10 剪纸送祝福语大全简短
11 长辈请客祝福语简短精辟
12 退伍祝福语老师简短
13 升学宴同学祝福语简短
14 鼠年同事祝福语简短创意
15 六一祝福语简短十六字
16 朋友祝福语卡片简短大气
17 英文祝福语 唯美简短的
18 元旦祝福语 简短独特群发