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0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20
*/
#include <stdio.h>
#include<stdlib.h>
void print1(int* det, int n);
int figure_serial1(int* det, int n);
int* det_son1(int* det, int a, int b, int n, int* c);
int main() {
int n;
printf("请输入n阶行列式的n的大小(n>0):");
scanf("%d", &n);
if (n > 0) {
}
else {
printf("输入错误!\n");
return 0;
}
printf("请输入n阶行列式:\n");
int* a = (int*)malloc(sizeof(int) * n * n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i * n + j]);
}
}
printf("行列式的计算结果是:%d\n", figure_serial1(a, n));
}
void print1(int* det, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", det[i * n + j]);
}
printf("\n");
}
printf("\n");
}
//串行计算2(一位数组计算)
int figure_serial1(int* det, int n)
{
int sum = 0;
if (n == 2) {
sum = det[0] * det[3] - det[1] * det[2];
return sum;
}
for (int i = 0; i < n; i++)//用第一行展开计算
{
int flag = -1;
if ((1 + i + 1) % 2 == 0) {
flag *= -1;
}
int* c = (int*)malloc(sizeof(int) * n * n);
int none = det[i] * figure_serial1(det_son1(det, 0, i, n, c), n - 1) * flag;
//printf("none的值是%d\n", none);
sum += none;
}
return sum;
}
//创建一维数组储存的行列式的余子式:
int* det_son1(int* det, int a, int b, int n, int* c)
{
int q = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == a || j == b)
{
}
else
{
c[q] = det[i * n + j];
q++;
}
}
}
// print1(c, n - 1);
return c;
}
用来算线代的行列式