qwq2519 @ 2021-10-04 09:08:27
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i(j);i<=k;++i)
#define drp(i,j,k) for(register int i(j);i>=k;--i)
#define repg(x) for(register int i(G.head[x]);i;i=G.next[i])
#define bug cout<<"~~~~~~~~~~~~~"<<'\n';
#define bugout(x) cout<<x<<'\n';
using std::cin;
using std::cout;
typedef long long lxl;
template<typename T>
inline T max( T a, T b) {
return a > b ? a : b;
}
template<typename T>
inline T min( T a, T b) {
return a < b ? a : b;
}
const double eps=1e-7;
const int N=57;
int n;
double a[N][N],ans[N];
int main() {
std::ios::sync_with_stdio(false);
cin>>n;
rep(i,1,n){
rep(j,1,n+1){
cin>>a[i][j];
}
}
rep(i,1,n){
int pos=i;
rep(j,i+1,n){
if(fabs(a[pos][i])<=fabs(a[j][i])) pos=j;
}
if(fabs(a[pos][i])<eps){
if(a[pos][n+1]) cout<<-1<<'\n';
else
cout<<0<<'\n';
return 0;
}
if(i!=pos) std::swap(a[i],a[pos]);
double div(a[i][i]);
rep(j,i,n+1){
a[i][j]/=div;
}
rep(j,i+1,n){
div=a[j][i];
rep(k,i,n+1){
a[j][k]-=a[i][k]*div;
}
}
}
ans[n]=a[n][n+1];
drp(i,n-1,1){
ans[i]=a[i][n+1];
rep(j,i+1,n){
ans[i]-=a[i][j]*ans[j];
}
}
rep(i,1,n){
cout<<"x"<<i<<"="<<std::fixed<<std::setprecision(2)<<ans[i]<<'\n';
}
return 0;
}
by qwq2519 @ 2021-10-04 09:32:31
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i(j);i<=k;++i)
#define drp(i,j,k) for(register int i(j);i>=k;--i)
#define repg(x) for(register int i(G.head[x]);i;i=G.next[i])
#define bug cout<<"~~~~~~~~~~~~~"<<'\n';
#define bugout(x) cout<<x<<'\n';
using std::cin;
using std::cout;
typedef long long lxl;
template<typename T>
inline T max( T a, T b) {
return a > b ? a : b;
}
template<typename T>
inline T min( T a, T b) {
return a < b ? a : b;
}
const double eps = 1e-7;
const int N = 57;
int n;
double a[N][N], ans[N];
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
rep(i, 1, n) {
rep(j, 1, n + 1) {
cin >> a[i][j];
}
}
rep(i, 1, n) {
int pos = i;
rep(j, i + 1, n) {
if(fabs(a[pos][i]) <= fabs(a[j][i])) pos = j;
}
if(i != pos) std::swap(a[i], a[pos]);
if(fabs(a[i][i]) < eps) {
if(fabs(a[i][n + 1] ) > eps)
cout << -1 << '\n';
else
cout << 0 << '\n';
return 0;
}
double div(a[i][i]);
rep(j, i, n + 1) {
a[i][j] /= div;
}
rep(j, i + 1, n) {
div = a[j][i];
rep(k, i, n + 1) {
a[j][k] -= a[i][k] * div;
}
}
}
ans[n] = a[n][n + 1];
drp(i, n - 1, 1) {
ans[i] = a[i][n + 1];
rep(j, i + 1, n) {
ans[i] -= a[i][j] * ans[j];
}
}
rep(i, 1, n) {
cout << "x" << i << "=" << std::fixed << std::setprecision(2) << ans[i] << '\n';
}
return 0;
}
by qwq2519 @ 2021-10-04 10:37:49
我发现
int pos = i;
rep(j, i + 1, n) {
if(fabs(a[pos][i]) <= fabs(a[j][i])) pos = j;
}
等于号去掉WAon#4,8 不去掉WA on#8,9 ..所以随机化?
by qwq2519 @ 2021-10-04 10:58:18
现在又改了,变成WA on#9,#10