【ZhengQing】[1125]Upper Triangular Matrix Check

Problem Description

上三角矩阵的判断
Time limit: 1 SecMemory limit: 128 MB

Description

Write a program that inputs a positive integer n (1<=n<=10) and the elements of an n×n square matrix a. If a is an upper triangular matrix, output “YES”; otherwise, output “NO". An upper triangular matrix is a matrix in which all elements below the main diagonal (excluding the main diagonal) are 0. The main diagonal is the line from the upper-left corner to the lower-right corner of the matrix. It is required to define a function IsUpperTriMatrixO to determine whether matrix a is an upper triangular matrix; return 1 if it is, otherwise return 0. The function prototype is as follows: IsUpperTriMatrix(int a, int n);

Input

Input a positive integer n (1<=n<=10) and the elements of an n×n square matrix a; all are integers.

Output

If a is an upper triangular matrix, output “YES"; otherwise, output “NO”

Sample Input

1
2
3
4
5
4
1 2 3 4
0 2 2 2
0 0 3 4
0 0 0 4

Sample Output

1
YES

Problem Analysis

The problem explains what an upper triangular matrix is. The judgment itself is not difficult.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int n;
int map[12][12];
bool IsUpperTriMatrix() {
    for(int i=1; i<n; i++)
        for(int j=0; j<i; j++)
            if(map[i][j])
                return false;
    return true;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                scanf("%d",&map[i][j]);
        printf("%s\n",IsUpperTriMatrix()?"YES":"NO");
    }
    return 0;
}

Problem address:[ZZULI] [1125] Upper Triangular Matrix Judgment

Loading...