c++ - 计算行列式。从主对角线 X 的每个元素中减去
问题描述
我的程序计算矩阵的行列式。
方阵大小 = 3 源矩阵
1 2 3 4 5 6 -> и вычесть из каждого элемента главной диагонали X 7 8 9
1-x 2 3 4 5-x 6 ->решить определитель 7 8 9-x
#include<iostream>
#include<math.h>
using namespace std;
int determinant(int matrix[10][10], int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant(submatrix, n - 1));
}
}
return det;
}
int main() {
int n, i, j,x;
int matrix[10][10];
cout << "Enter the size of the matrix:\n";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout << "The entered matrix is:" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
cout << "Determinant of the matrix is " << determinant(matrix, n);
return 0;
}
解决方案
推荐阅读
- html - 电子邮件签名中的图像
- python - Android adb sendevent 没有正确执行事件
- android - 无法创建到运行热点的 Android 设备的套接字连接
- javascript - Update Chart when add/remove records Ext JS
- html - 带类的元素内的最后一个带类的元素的 CSS 选择器
- jquery - 在 iOS 上的 Safari 中滚动 Bootstrap 导航栏不起作用
- java - W/Firestore:[CustomClassMapper]:Android 类没有设置器/字段
- biztalk - 无法加载文件或程序集
- c# - CRM 插件跟踪日志 - 仅用于沙盒模式的日志
- excel - 如何从 XLS 文件的所有文本框中清除文本,而不删除这些框?