该程序如何工作?

Thi*_*ker -2 c

我找到了一个C程序来查找3x3矩阵的行列式,但我不知道它是如何工作的。

我对这部分感到困惑

for(i=0; i<3; i++) {
    k = k + a[0][i] * (a[1][(i+1)%3] * a[2][(i+2)%3] - a[1][(i+2)%3] * a[2][(i+1)%3]);
}    
Run Code Online (Sandbox Code Playgroud)

3x3矩阵的行列式公式

a b c  
d e f  
g h i  
Run Code Online (Sandbox Code Playgroud)

行列式= a(ei-fh)-b(di-fg)+ c(dh-eg)

此处的公式具有-b(负b),但是代码中使用的公式中没有负数,所以它如何工作?

#include<stdio.h>

int main()
{
    int a[10][10], i, j, k=0;

    printf("Enter values of matrix: \n\n");
    for(i=0; i<3; i++) {
        for(j=0; j<3; j++) {
            printf("Value of [%d][%d]: ", i, j);
            scanf(" %d", &a[i][j]);
        }
    }

    printf("Matrix is:\n\n");
    for(i=0; i<3; i++) {
        for(j=0; j<3; j++) {
            printf("%d  ", a[i][j]);
        }
        printf("\n\n");
    }

    for(i=0; i<3; i++) {
        k = k + a[0][i] * (a[1][(i+1)%3] * a[2][(i+2)%3] - a[1][(i+2)%3] * a[2][(i+1)%3]);
    }

    printf("Determinant of the matrix is: %d \n", k);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*ger 6

该算法利用了- b * (d * i - f * g)等于的事实+ b * (f * g - d * i)。你的

行列式= a(ei-fh)-b(di-fg)+ c(dh-eg)

变成

行列式= a(ei-fh)+ b(fg-di)+ c(dh-eg)

通过使用%3循环回到行末尾的开始,每个术语都具有相同的结构。