高斯消元

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define dbg(x) cout<<#x<<"="<<x<<'\n'
#define mmt(x,y) memset(x,y,sizeof(x))
#define maxn 200
int n;
double a[maxn][maxn];
bool gauss()
{
fo(i,1,n)
{
R int k=i;
fo(j,i+1,n)if(abs(a[j][i])>abs(a[k][i]))
k=j;
if(abs(a[k][i])<1e-8)return 1;
swap(a[i],a[k]);
R double now=a[i][i];
fo(j,1,n+1)a[i][j]/=now;
a[i][i]=1;
fo(k,1,n)
if(k!=i)
{
R double now=a[k][i];
fo(j,i,n+1)a[k][j]-=a[i][j]*now;
}
}
return 0;
}
int main()
{
scanf("%d",&n);
fo(i,1,n)
fo(j,1,n+1)
scanf("%lf",&a[i][j]);
if(gauss())puts("No Solution");
else fo(i,1,n)printf("%.2lf\n",a[i][n+1]);
return 0;
}
显示 Gitment 评论