在C ++中检查给定的矩阵是否为Hankel
假设我们有一个正方形矩阵,我们的任务是检查矩阵是否为汉克尔矩阵。Hankel矩阵是一个正方形矩阵,其中每个从左到右的倾斜斜对角元素都是恒定的。假设矩阵如下-
为了检查矩阵是否为汉克尔矩阵,我们必须检查mat[i,j]=ai+j。一个I+J可以被定义为-
$$a_{i+j}=\开始{cases}mat[i+j,0]<n\\mat[i+j-n+1,n-1]否则\end{cases}$$
示例
#include <iostream> #define N 5 using namespace std; bool isHankelMat(int mat[N][N], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i + j < n) { if (mat[i][j] != mat[i + j][0]) return false; } else { if (mat[i][j] != mat[i + j - n + 1][n - 1]) return false; } } } return true; } int main() { int n = 5; int mat[N][N] = { { 1, 2, 3, 4, 5}, { 2, 3, 4, 5, 6}, { 3, 4, 5, 6, 7}, { 4, 5, 6, 7, 8}, { 5, 6, 7, 8, 9} }; if(isHankelMat(mat, n)) cout << "This is Hankel Matrix"; else cout << "This is not Hankel Matrix"; }
输出结果
This is Hankel Matrix