zoukankan      html  css  js  c++  java
  • C语言求行列式的值

    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    #define NUM 3 
    
    
    int Fun(int n, int a[NUM][NUM]);            /*函数声明*/
    int main()
    {
        
        int i = 0, j = 0;                    /*i,j分别表示行与列*/
        int a[3][3] = { {14,6,2},{6,3,3},{5,9,8} };             /*定义行列式*/           
        printf("%d
    ", Fun(NUM, a));
        system("pause");
        return 0;
    }
    
    /*以下为计算行列式值的递归函数*/
    int Fun(int n, int a[NUM][NUM])
    {
        static int b[NUM][NUM] = { { 0 } };         /*定义数组b并初始化*/
        int i = 0, j = 0, sum = 0;          /*i,j为行与列,sum为行列式的值*/
        int x = 0, c = 0, p = 0;                /*用x判断加与减,c,p为中间变量*/
        if (n == 1)
            return a[0][0];
    
        for (i = 0; i < n; i++)               /*此处大循环实现将余子式存入数组b中*/
        {
            for (c = 0; c < n - 1; c++)
            {
                for (j = 0; j < n - 1; j++)
                {
                    if (c < i) {             /*借助c判断每行的移动方法*/
                        p = 0;              /*当p=0时,行列式只向左移,即消去对应的第一列的数*/
                    }
                    else {                   /*否则行列式左移后再上移*/
                        p = 1;
                    }
                    b[c][j] = a[c + p][j + 1];
                }
            }
            if (i % 2 == 0) {                  /*i+j(此时j=0,故只考虑i)为偶数,加法预算*/
                x = 1;
            }
            else {                           /*i+j为奇数,减法运算*/
                x = (-1);
            }
            sum += a[i][0] * Fun(n - 1, b) * x;         /*计算行列式的值*/
        }
        return sum;                                   /*将值返回*/
    }
  • 相关阅读:
    django项目环境搭建备忘
    Python IDE的选择和安装
    MAC上python环境搭建
    hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
    ubuntu下hadoop完全分布式部署
    ubuntu下集群设置静态ip
    C语言调用库函数实现生产者消费者问题
    poj 1703(带权并查集)
    poj 1330
    poj1724
  • 原文地址:https://www.cnblogs.com/junjunjun123/p/9180805.html
Copyright © 2011-2022 走看看