题解 | 上三角矩阵判定
上三角矩阵判定
https://www.nowcoder.com/practice/f5a29bacfc514e5a935723857e1245e4
#include <iostream>
using std::cin, std::cout;
using std::cerr, std::endl;
using std::ios;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n) || n < 1) {
// cerr 缓冲区大小为0,报错直接输出,所以不用写std::endl;
std::cerr << "Invalid input: n";
return 0;
}
int val;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// 接收并判断数据
if (!(cin >> val)) {
std::cerr << "Invalid input: val";
return 0;
}
// 若不为上三角矩阵,提前退出程序
if (i > j && val != 0) {
cout << "NO" << endl;
return 0;
}
}
}
cout << "YES" << endl;
return 0;
}

查看18道真题和解析