C ++中的双对称矩阵?
在这里,我们将看到一个程序,该程序将有助于检查矩阵是否是双对称的。双对称矩阵是一个关于两个主要对角线对称的正方形矩阵。下面的矩阵是双对称矩阵的示例。
1 2 3 4 5 2 6 7 8 4 3 7 9 7 3 4 8 7 6 2 5 4 3 2 1
算法
checkBiSymmetric(mat,n)
Begin
for i in range 0 to n – 1, do
for j in range 0 to i – 1, do
if mat[i, j] is not same as mat[j, i], then
return false
end if
done
done
for i in range 0 to n – 1, do
for j in range 0 to n – i, do
if mat[i, j] is not same as mat[n – j - 1, n – i - 1], then
return false
end if
done
done
return true
End示例
#include<iostream>
#define N 5
using namespace std;
int matrix[N][N] = {{1, 2, 3, 4, 5},
{2, 6, 7, 8, 4},
{3, 7, 9, 7, 3},
{4, 8, 7, 6, 2},
{5, 4, 3, 2, 1}};
bool checkBiSymmetric() {
for (int i = 0; i < N; i++) //scan through forward diagonal
for (int j = 0; j < i; j++)
if (matrix[i][j] != matrix[j][i]) //when corresponding elements are not same, return false
return false;
for (int i = 0; i < N; i++) //scan through forward diagonal
for (int j = 0; j < N - i; j++)
if (matrix[i][j] != matrix[N - j - 1][N - i - 1]) //when corresponding elements are not same, return false
return false;
return true; //otherwise return true
}
main() {
if(checkBiSymmetric()){
cout << "Yes the matrix is bisymmetric";
} else {
cout << "No the matrix is not bisymmetric";
}
}输出结果
Yes the matrix is bisymmetric
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便