标签:algorithm iostream ace i+1 alt str abs stream img
高斯消元用于求解线性方程。
贴一下代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 150
int n;
double a[maxn][maxn];
using namespace std;
void gau(){
for(int i=1;i<=n;i++){
int line=i;
for(int j=i+1;j<=n;j++){
if(fabs(a[line][i])<fabs(a[j][i]))line=j;
}
for(int j=1;j<=n+1;j++)swap(a[line][j],a[i][j]);
if(!a[i][i]){printf("No Solution");return;}
for(int j=n+1;j>=i+1;j--){
for(int k=i+1;k<=n;k++){
a[k][j]-=a[k][i]/a[i][i]*a[i][j];
}
}
}
for(int i=n;i>=1;i--){
for(int j=i+1;j<=n;j++){
a[i][n+1]-=a[j][n+1]*a[i][j];
}
a[i][n+1]/=a[i][i];
}
for(int i=1;i<=n;i++){
printf("%.2f\n",a[i][n+1]);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n+1;j++){
scanf("%lf",&a[i][j]);
}
}
gau();
}
标签:algorithm iostream ace i+1 alt str abs stream img
原文地址:https://www.cnblogs.com/GavinZheng/p/11067621.html