이 튜토리얼에서는 내림차순으로 정렬된 행렬의 열 수를 찾는 프로그램에 대해 설명합니다.
이를 위해 매트릭스가 제공됩니다. 우리의 임무는 내림차순으로 정렬된 요소가 있는 행렬의 열 수를 계산하는 것입니다.
예시
#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