对称矩阵

题目截图:

思路:

  遍历矩阵的下三角,判断与对称部分是否相同即可。

代码如下:

 1 /*
 2     对称矩阵 
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 #define maxn 101
13 int rec[maxn][maxn];                             // 用来存储矩阵 
14 
15 int main() {
16     int n, i, j;
17     while(scanf("%d", &n) != EOF) {
18         for(i=0; i<n; ++i) {
19             for(j=0; j<n; ++j) {
20                 scanf("%d", &rec[i][j]);        // 输入矩阵 
21             }
22         }
23         int flag = 1;                            // 1 表示为对称,0 表示为不对称 
24         for(i=1; i<n; ++i) {                    // 遍历矩阵的下三角 
25             for(j=0; j<i; ++j) {
26                 if(rec[i][j] != rec[j][i]) {    // 若对称位置不相等 
27                     flag = 0;                    // 更新标志位 
28                     break;                        // 退出循环 
29                 }
30             }
31             if(!flag) {                            // 若该行存在不对称的元素 
32                 break;                            // 退出循环 
33             }
34         }
35         if(flag) {                                // 按要求输出 
36             printf("Yes!
");                    // 对称 
37         } else {
38             printf("No!
");                    // 不对称 
39         }
40     }
41 
42     return 0;
43 }
原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest17.html