이 튜토리얼에서는 행렬에서 정렬된 모든 행의 수를 찾는 프로그램에 대해 설명합니다.
이를 위해 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