高斯 & 约旦 消元模板
作者:互联网
题面
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 105;
double a[N][N]; int n;
bool gauss() {
for(int i = 1; i <= n; i++) {
int maxn = i;
for(int j = i + 1; j <= n; j++)
if(fabs(a[j][i]) > fabs(a[maxn][i]))
maxn = j;
for(int j = 1; j <= n + 1; j++)
swap(a[i][j],a[maxn][j]);
if(!a[i][i]) return false;
for(int j = 1; j <= n; j++) {
if(j == i) continue;
double tmp = a[j][i] / a[i][i];
for(int k = i + 1; k <= n + 1; k++)
a[j][k] -= a[i][k] * tmp;
}
}
return true;
}
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]);
if(!gauss()) puts("No Solution");
else {
for(int i = 1; i <= n; i++)
printf("%.2lf\n",a[i][n + 1] / a[i][i]);
}
return 0;
}
标签:std,int,double,maxn,约旦,消元,include,模板 来源: https://blog.csdn.net/init1064573673/article/details/116276254