题解 | 上三角矩阵判定

上三角矩阵判定

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;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务