C语言 数组 对称 对角线

这个问题,希望可以帮助一下已知M行N列二维0,1数组A,为某个二元关系的关系矩阵,判断该关系的性质。若该数组的主对角线都是1,则该关系是自反的;若该数组主对角线都是0,则该关系是反自反的;若该数组是对称数组则关系是对称的;若该数组中除主对角线以外,a=1,则a;=0.则该关系是反对称的。编程实现对关系性质的判断。例如:
数组:1010
0111
1111
0111
输出该关系是自反的对称的

基于Monster 组和GPT的调写:


#include <stdio.h>

int main() {
    int M = 4, N = 4; // 数组的行数和列数
    int A[M][N] = {{1, 0, 1, 0},
                   {0, 1, 1, 1},
                   {1, 1, 1, 1},
                   {0, 1, 1, 1}}; // 定义关系矩阵数组A
    int i, j;
    int is_reflexive = 1, is_irreflexive = 1, is_symmetric = 1, is_antisymmetric = 1;
    // is_reflexive、is_irreflexive、is_symmetric、is_antisymmetric分别表示关系是否自反、反自反、对称、反对称
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            if (i == j) { // 主对角线
                if (A[i][j] == 0) {
                    is_reflexive = 0;
                    is_antisymmetric = 0;
                } else {
                    is_irreflexive = 0;
                }
            } else { // 非主对角线
                if (A[i][j] != A[j][i]) {
                    is_symmetric = 0;
                    is_antisymmetric = 0;
                }
                if (A[i][j] == 1 && A[j][i] == 1) {
                    is_irreflexive = 0;
                }
                if (A[i][j] == 1 && A[j][i] == 1 && i != j) {
                    is_reflexive = 0;
                }
                if (A[i][j] == 1 && A[j][i] == 1 && i < j) {
                    is_antisymmetric = 0;
                }
            }
        }
    }
    // 根据is_reflexive、is_irreflexive、is_symmetric、is_antisymmetric的值输出关系性质
    if (is_reflexive) {
        printf("该关系是自反的");
    }
    if (is_irreflexive) {
        printf("该关系是反自反的");
    }
    if (is_symmetric) {
        printf("该关系是对称的");
    }
    if (is_antisymmetric) {
        printf("该关系是反对称的");
    }
    return 0;
}

img