Exercise 7-3 judging upper triangular matrix

Time:2021-1-17

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

This problem requires the preparation of procedures to determine whether a given square matrix is a triangular matrix.

Input format:

Enter the first line to give a positive integerT. Is the number of matrices to be measured. Next, we give theT matrix information: the first line of each matrix information gives a positive integer no more than 10n。 subsequentlyN lines, each givenN integers separated by spaces.

Output format:

The judgment result of each matrix occupies one line. If the input matrix is an upper triangular matrix, output “yes”, otherwise output “no”.

 

The above is the topic requirements. Next, the code is given

1 #include
 2 #include
 3 #include
 4 # include // some. H are not used, but they are added out of habit and without engineering pressure.
 5 int main() {
 6 int n = 0; // N is the number of the whole matrix
 7 int * m; // M is the horizontal or vertical number of the nth matrix (square matrix) [M is an array]
 8 int a = 0; // A is the judgment condition, which will be used later in the if statement
 9 // int interval = 0; // irrelevant elements, please ignore them automatically
10 int boo [10]; // put the judgment of each matrix into this array
11     scanf("%d", &n);  
12 m = (int *) malloc (n * sizeof (int)); // dynamic array  
13     
14     for (int l = 0; l < n; l++) {
15 boo [l] = 1; // assume that all upper triangular matrices are followed by 0 if they are inconsistent
16         scanf("%d", &m[l]);  
17         int len = pow(m[l],2);
18         for (int i = 0; i < m[l]; i++) {
19             for(int j = 0 ; j < m[l] ; j++){
20                 scanf("%d", &a);
21                 if (i > j) {
22                     if (a != 0) {
23 boo [l] = 0; // here! My idea is online processing
24                     }
25                 }
26             
27             }
28         }
29     }
30     for (int i = 0; i < n; i++) {
31         if (boo[i] == 1) {
32             printf("YES\n");
33         }
34         else
35             printf("NO\n");
36     }
37     return 0;
38     
39 }

The above is the code, next step by step analysis.

I learned C in my freshman year. This time I want to learn C again. I want to find my own code through C. After all, I have been studying theory courses for a year, such as operating system, data structure, network and composition principle, which delays the code. This time I try to make it up and refuel from different directions.

 

My core idea of this problem is online processing, that is, input a number, if it meets the conditions, make a judgment, and save the judgment result in the boo array.

 

Then I need to think a little about how to represent “0” below the diagonal. I used a stupid method, that is, I used two for nesting, the outer layer is abscissa, the inner layer is ordinate, and then abscissa > ordinate, that is, the elements below the diagonal.

 

Write about your experience

1. This code can be refined. If you follow my idea, you can solve the problem of element position without using two layers of for. You can use mathematical methods to solve it skillfully. However, baby is stupid. After thinking for a long time, he can’t think of a clever method, or he can define a lot of data. I don’t like this way, so I use this method. If brothers have a good method, you can say it in the comment area or in private Any letter will do. I will solve it in the first time.

2. Junior, ready to take the postgraduate entrance examination, why learn C, because it test C, on the one hand, I want to pick up, on the other hand, I also want to test it. Come on from now on!

Recommended Today

Building rest service with dubbox / Dubbo + Spring + mybatis + gradle

Miscellaneous miscellaneous Personal blog:Program ape of diemengOriginal text:Building rest service with dubbox / Dubbo + Spring + mybatis + gradleStatement:Due to the automatic verification of SF editing, some English words are wrong, such as:gradleAutomatically replaced by editor withgrade,jdkReplace withidkWhen checking, please translate by yourself. ——————————Multi map early warning—————————— What is dubbox? See what others say: […]