Go 程序检查对称矩阵

编写一个 Go 程序来检查给定的矩阵是否为对称矩阵。任何在转置后保持不变的方阵都称为对称矩阵。在此 Go 示例中,我们转置给定矩阵,然后将原始矩阵中的每个元素与转置矩阵进行比较。如果它们相等,则为对称矩阵。

package main

import "fmt"

func main() {
    var i, j, rows, columns int

    var symmMat [10][10]int
    var transMat [10][10]int

    fmt.Print("Enter the Matrix rows and Columns = ")
    fmt.Scan(&rows, &columns)

    fmt.Println("Enter Matrix Items to Transpose = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&symmMat[i][j])
        }
    }
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            transMat[j][i] = symmMat[i][j]
        }
    }
    count := 1
    for i = 0; i < columns; i++ {
        for j = 0; j < rows; j++ {
            if symmMat[i][j] != transMat[i][j] {
                count++
                break
            }
        }
    }
    if count == 1 {
        fmt.Println("This Matrix is a Symmetric Matrix")
    } else {
        fmt.Println("The Matrix is Not a Symmetric Matrix")
    }

}
Enter the Matrix rows and Columns = 2 2
Enter Matrix Items to Transpose = 
1 2
2 1
This Matrix is a Symmetric Matrix

Golang 程序使用 For 循环范围检查矩阵是否为对称矩阵。

package main

import "fmt"

func main() {

    var symmMat [3][3]int
    var transMat [3][3]int

    fmt.Println("Enter Matrix Items to Transpose = ")
    for i, rows := range symmMat {
        for j := range rows {
            fmt.Scan(&symmMat[i][j])
        }
    }
    for i, rows := range symmMat {
        for j := range rows {
            transMat[j][i] = symmMat[i][j]
        }
    }
    count := 1
    for i, rows := range symmMat {
        for j := range rows {
            if symmMat[i][j] != transMat[i][j] {
                count++
                break
            }
        }
    }
    if count == 1 {
        fmt.Println("This Matrix is a Symmetric Matrix")
    } else {
        fmt.Println("The Matrix is Not a Symmetric Matrix")
    }
}
Golang Program to Check Symmetric Matrix