Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

นับคอลัมน์ที่เรียงลำดับลดในเมทริกซ์ใน 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[j - 1][i] >= mat[j][i])
                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