#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps = 1e-6;
const int maxn = 100;
double a[maxn][maxn];
double x[maxn],y[maxn],b[maxn];
double l[maxn][maxn],u[maxn][maxn];
void solve2 (int n){
for (int i=1;i<=n;i++) l[i][i]=1;
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++){
double tmp = a[i][j];
for (int k=1;k<i;k++){
tmp -= l[i][k]*u[k][j];
}
u[i][j]=tmp;
}
for (int j=i+1;j<=n;j++){
double tmp = a[j][i];
for (int k=1;k<i;k++){
tmp -= l[j][k]*u[k][i];
}
l[j][i]=tmp/u[i][i];
}
}
for (int i=1;i<=n;i++){
double tmp = y[i];
for (int j=1;j<i;j++){
tmp -= b[j]*l[i][j];
}
b[i]=tmp;
}
for (int i=n;i>=1;i--){
double tmp = b[i];
for (int j=n;j>i;j--){
tmp -= u[i][j]*x[j];
}
x[i]= tmp/u[i][i] ;
}
}
int main (){
int n;
cin >> n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
scanf ("%lf",&a[i][j]);
}
scanf ("%lf",&y[i]);
}
solve2(n);
for (int i=1;i<n;i++){
printf("x%d = %lf, ", i,x[i]);
}
printf("x%d = %lf\n",n,x[n] );
}
矩阵计算
最新推荐文章于 2022-06-20 15:58:30 发布