gpt4 book ai didi

c++ - 矩阵的传递性

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:16 27 4
gpt4 key购买 nike

我正在尝试实现一个 C++ 解决方案来确定 n*n 元素的二进制矩阵是否可传递。这是条件:

A matrix M is transitive if and only if for any elements a, b, c (a != b != c) such that M[a][b] = 1 and M[b][c] = 1 the condition M[a][c] = 1 is true.

如果矩阵具有传递性,则输出应为 1,否则为 0。

我的代码:

#include<iostream> 
using namespace std;

int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

int n;
bool trans = true;
cin >> n;

int **m = new int*[n];
for (int i = 0; i < n; i++) {
m[i] = new int[n];
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> m[i][j];
}
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (m[i][j] == 1 && m[j][i] == 1 && m[i][i] != 1)
trans = false;
}
}

if (trans)
cout << 1;
else
cout << 0;
}

代码没有通过我试图提交给的电子法官。可能是什么问题?

最佳答案

您需要 3 个 for 循环而不是 2 个(i、j 和 k)。

关于c++ - 矩阵的传递性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com