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 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 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;
	}



用来算线代的行列式

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值