Triangle determinant judgment

Title: 7-2 basic programming problem _ Learning Exercise Exercise 7- 3] [three] [-3] judgment must be two-dimensional array on the triangular matrix (15 points)

The upper triangular matrix means below the main diagonal elements of the matrix are 0; main diagonal line from the upper left corner to the lower right corner of the matrix.

This question requires the preparation of procedures to determine whether a given square upper triangular matrix.

Input format:
input of the first row is given a positive integer T, the number of the test matrix. Information of the matrix T is given next: a first row of each matrix is given information is a positive integer not more than n 10 is. Then n lines of n integers are given, separated by a space therebetween.

Output format:
judgment result of each matrix of one row. If the matrix is an upper triangular matrix input, output "YES", otherwise a "NO".

Sample input:
2
. 3
. 1. 3 2
0. 4. 5
0 0. 6
2
. 1 0
-8 2
Output Sample:
YES
NO

Problem-solving ideas

The upper triangular matrix means below the main diagonal elements of the matrix are 0; main diagonal line from the upper left corner to the lower right corner of the matrix.
So long as the number of rows to determine whether the element is greater than the number of columns of all zeros, if there is not a zero, then it is not on the triangular matrix,
because the matrix, the title will be applied to two-dimensional array. Also required according to the subject, there may need to determine a plurality of matrices, the need
to cycle.

flow chart

problem

How to determine the array is not on the triangular matrix? Recording the input two-dimensional array by a matrix defined by a nested loop,
the first loop is used to remember the first few lines, for the second cycle are the columns referred to, when the element row is determined whether the column is greater than 0 .

Core code

Whether judged on the triangular matrix

for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                scanf("%d",&b[i][j]);
            }
        }
        for (i = 1; i < n; i++) {
            for (j = 0; j < i; j++) {
                if (b[i][j] != 0) {
                    g = 0;
                    break;
                }
            }
            if (g == 0) {
                break;
            }
        }
        if (g == 0) {
            printf("NO\n");
        }
        else {
            printf("YES\n");
        }

Guess you like

Origin www.cnblogs.com/Laiakuai/p/12048497.html