程序员

线代矩阵

作者:admin 2021-04-22 我要评论

线代矩阵 不要忘了改类名 ----4月4日 package com . hjh . function ; import java . util . Scanner ; public class test1 { public static void main ( String...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

线代矩阵

不要忘了改类名
----4月4日

package com.hjh.function;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
//        int[][] a = {
//                {3, 1, -1, 2},
//                {-5, 1, 3, -4},
//                {2, 0, 1, -1},
//                {1, -5, 3, -3},
//        };
//        det = 40;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入矩阵阶数:");
        int n = sc.nextInt();
        int[][] det = new int[n][n];
        System.out.println("请输入数据:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                det[i][j] = sc.nextInt();
            }
        }
        System.out.println("矩阵的值是: " + detValue(det));
    }

    public static int detValue(int[][] det) {
        int n = det.length;
        int sum = 0;
        if (n == 1) {
            return det[0][0];
        } else {
            for (int i = 0; i < n; i++) {
                if (i % 2 == 0) {
                    sum += det[i][0] * detValue(detUnfold(det, i));
                } else {
                    sum -= det[i][0] * detValue(detUnfold(det, i));
                }
            }
        }
        return sum;
    }

    // 行列式按第一列展开
    public static int[][] detUnfold(int[][] det, int r) {
        int n = det.length;
        int[][] dett = new int[n - 1][n - 1];
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (i < r) {
                    dett[i][j - 1] = det[i][j];
                } else if (i > r){
                    dett[i - 1][j - 1] = det[i][j];
                }
            }
        }
        return dett;
    }
}

;原文链接:https://blog.csdn.net/qq_51626275/article/details/115433748

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 天津智慧物联赛道明星结营丨创业不孤独

    天津智慧物联赛道明星结营丨创业不孤独

  • 阿里新零售赛道明星结业,群星集结相伴

    阿里新零售赛道明星结业,群星集结相伴

  • 教育行业优质解决方案分享【智慧校园解

    教育行业优质解决方案分享【智慧校园解

  • Excelize 2.4.0 正式版发布, 新增 152

    Excelize 2.4.0 正式版发布, 新增 152

腾讯云代理商
海外云服务器