大学数学分为三大块:1.高等数学 2.线性代数 3.概率统计
N阶行列式就是属于线性代数中的知识! 程序并不是很完善,但基本的逻辑清晰,上代码......
import java.util.Scanner;
/**
*
* @author 慕一春
* @version 1.0.0
* @filename DetN.java
* @time 2016-7-21 下午12:39:00
* @copyright(C) 2016
*/
/*
4
4 1 4 8
1 1 3 2
2 2 5 1
2 2 1 4
*/
public class DetN {
public static int n = 0; // 阶级
public static double arr[][]; // 行列式
public static double detResult = 1; // 结果
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
arr = new double[n][n];
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[0].length; j++){
arr[i][j] = sc.nextDouble();
}
}
arrFunction(); // 转为下三角行列式
displayResult(); // 显示结果
}
/**
* 将行列式转为下三角行列式
* @author 慕一春
*/
public static void arrFunction(){
double temp = 0;
for (int i = 0; i< arr.length - 1; i++) {
for (int row = i + 1; row < arr.length; row++) {
temp = arr[row][i] / arr[i][i];
arr[row][i] = 0;
for (int col = i + 1; col < arr.length; col++) {
arr[row][col] = arr[row][col] - temp * arr[i][col];
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if (i == j) {
detResult = detResult * arr[i][j];
}
}
}
}
/**
* 显示结果
* @author 慕一春
*/
public static void displayResult(){
for (int i = 0; i< arr.length; i++) {
for (int j = 0; j< arr[0].length; j++) {
System.out.print (arr[i][j]+"\t");
}
System.out.println();
}
System.out.println ("The result is: "+detResult);
}
}
输入:
3
5 2 1
1 2 5
34 1 34
输出:
5.0 2.0 1.0
0.0 1.6 4.8
0.0 0.0 65.0
The result is: 520.0