题解 | 上三角矩阵判定
上三角矩阵判定
https://www.nowcoder.com/practice/f5a29bacfc514e5a935723857e1245e4
思路:二维数组中,对角线下方的元素第一个索引值会大于第二个索引值,设置一个num,如果第一个索引值会大于第二个索引值的元素不为0,num++,最后根据num是否为0来输出yes或no
#include <iostream>
using namespace std;
int main() {
int m, n;
int index;
cin >> m;
int num = 0;
int arr[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cin >> n;
arr[i][j]=n;
if ( i > j && arr[i][j] != 0) {
num++;
}
}
}
if (num == 0) {
cout << "YES";
} else {
cout << "NO";
}
}
// 64 位输出请用 printf("%lld")

查看1道真题和解析