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

更新於:2020 年 2 月 17 日

133 次瀏覽

啟動您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.