请用C语言写一矩阵求逆
时间: 2023-03-14 19:21:58 浏览: 98
juzhenqiuni.rar_求逆矩阵C语言_矩阵求逆
很高兴为您服务,下面是用C语言求一矩阵求逆的代码:
#include <stdio.h> int main() { int n; int i, j; double matrix[60][60]; printf("请输入矩阵的阶数:"); scanf("%d", &n); printf("请输入矩阵的元素:"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%lf", &matrix[i][j]); } } double inverse[60][60]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { inverse[i][j] = matrix[i][j]; } } double b[60][60]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) b[i][j] = 1; else b[i][j] = 0; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { double l = inverse[j][i] / inverse[i][i]; for (int k = 0; k < n; k++) { inverse[j][k] = inverse[j][k] - inverse[i][k] * l; b[j][k] = b[j][k] - b[i][k] * l; } } } } for (i = 0; i < n; i++) { double a = inverse[i][i]; for (j = 0; j < n; j++) { inverse[i][j] = inverse[i][j] / a; b[i][j] = b[i][j] / a; } } printf("矩阵的逆矩阵为:"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%lf ", b[i][j]); } printf(""); } return 0; }
阅读全文