endswitch @ 2024-07-10 13:51:04
rt。
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 1e2 + 5;
const double eps = 1e-6;
inline int read(){
register char ch = getchar();
register int x = 0;
register bool f = 0;
while(!isdigit(ch)) f |= (ch == '-') , ch = getchar();
while(isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48) , ch = getchar();
return f ? -x : x;
}
int n;
double a[N][N];
inline int Gauss(){
bool flag = 0;
int nowline = 1;
for(register int i = 1 ; i <= n ; ++ i) {
for(register int j = i ; j <= n ; ++ j)
if(fabs(a[j][i]) > eps) {
swap(a[i] , a[j]);
break;
}
if(fabs(a[i][i]) < eps) {
flag = 1;
continue;
}
//for(register int j = 1 ; j <= n + 1 ; ++ j) swap(a[newline][j] , a[nowline][j]);
for(register int j = n + 1; j >= i ; -- j) a[i][j] /= a[i][i];
for(register int j = i + 1 ; j <= n ; ++ j)
for(register int k = n + 1 ; k >= i ; -- k)
a[j][k] -= a[i][k] * a[j][i];
//++ nowline;
}
if(flag) {
for(register int i = 1 ; i <= n ; ++ i){
bool yg = 0;
for(register int j = 1 ; j <= n ; ++ j) {
if(a[i][j] != 0) {
yg = 1;
break;
}
}
if(yg) continue;
if(fabs(a[i][n + 1]) > eps) return -1;
}
return 0;
}
for(register int i = n ; i ; -- i)
for(register int j = i + 1 ; j <= n ; ++ j)
a[i][n + 1] -= a[j][n + 1] * a[i][j];
return 1;
}
int main(){
n = read();
for(register int i = 1 ; i <= n ; ++ i)
for(register int j = 1 ; j <= n + 1 ; ++ j)
a[i][j] = read();
int backin = Gauss();
if(backin == 1)
for(register int i = 1 ; i <= n ; ++ i)
printf("x%d=%.2f\n" , i , a[i][n + 1]);
else printf("%d" , backin);
return 0;
}
by Vegetableless @ 2024-07-10 14:03:50
hack:
4
0 0 2 1 2
0 0 1 1 1
0 0 0 1 1
0 0 0 0 0
方程无解不仅仅是只有