Daniel_yao @ 2024-07-17 18:31:34
#include<bits/stdc++.h>
#define ll long long
#define For(i,l,r) for(int i=l;i<=r;++i)
#define FOR(i,r,l) for(int i=r;i>=l;--i)
#define eqs 1e-10
using namespace std;
const int N = 105;
int n;
double a[N][N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
For(i,1,n) {
For(j,1,n+1) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n - 1; ++i) {
For(j,i+1,n) if(fabs(a[i][i]) < fabs(a[j][i])) swap(a[i], a[j]);
if(fabs(a[i][i]) < eqs) continue;
For(j,i+1,n) {
double w = a[j][i] / a[i][i];
For(l,i,n+1) {
a[j][l] -= w * a[i][l];
}
}
}
for (int i = n; i >= 2; --i) {
if(fabs(a[i][i]) < eqs) continue;
FOR(j,i-1,1) {
double w = a[j][i] / a[i][i];
For(l,i,n+1) {
a[j][l] -= w * a[i][l];
}
}
}
For(i,1,n) if(fabs(a[i][i]) < eqs && fabs(a[i][n+1]) > eqs) return puts("-1"), 0;
For(i,1,n) if(fabs(a[i][i]) < eqs && fabs(a[i][n+1]) < eqs) return puts("0"), 0;
For(k,1,n) {
printf("x%d=%.2lf\n", k, a[k][n+1] / a[k][k]);
}
return 0;
}
/*
9 3 2 2
0 3.66667 6.77778 2.77778
0 0 -1.15152 2.75758
9 0 2 -13.5526
0 3.66667 0 19.0088
0 0 -1.15152 2.75758
*/
by Max_hmt @ 2024-07-17 18:40:04
@Daniel_yao
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n ;
double a[110][110] ;
inline int read()
{
int x=0,f=1; char c=getchar();
while(c>'9'||c<'0') {if(c=='-') f=-1; c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
return f*x;
}
void gauss()
{
for ( int i = 1 ; i <= n; ++i )
{
for ( int j = i + 1 ; j <= n ; ++j )
if ( fabs(a[j][i]) > fabs(a[i][i]) )
for ( int k = 1 ; k <= n + 1 ; ++k )
swap( a[i][k] , a[j][k] ) ;
if ( fabs(a[i][i]) < 1e-8 ) continue ;
for ( int j = 1 ; j <= n ; ++j )
{
if ( i == j ) continue ;
if ( fabs(a[j][i]) < 1e-8 ) continue ;
double r = a[i][i] / a[j][i] ;
for ( int k = 1 ; k <= n + 1 ; ++k )
a[j][k] = a[i][k] - a[j][k] * r ;
}
}
}
int main()
{
n = read() ;
for ( int i = 1 ; i <= n ; ++i )
for ( int j = 1 ; j <= n + 1 ; ++j )
scanf("%lf",&a[i][j]) ;
gauss() ;
int ok = 0 ;
for ( int i = 1 ; i <= n ; ++i )
if ( fabs(a[i][i]) < 1e-8 && fabs(a[i][n+1]) < 1e-8 )
{
ok = 1 ; break ;
}
if ( ok ) printf("No Solution\n") ;
else for ( int i = 1 ; i <= n ; ++i ) printf("%.2lf\n",a[i][n+1] / a[i][i]) ;
}
@Daniel_yao
by Daniel_yao @ 2024-07-17 19:05:52
@Max_hmt 不是这一道题qwq,而且我并不知道我的代码哪里错啦qwq
by Max_hmt @ 2024-07-17 19:17:13
@Daniel_yao 稍等,大佬,让蒟蒻康康