liyixin0514 @ 2024-10-23 21:15:14
rt.评测记录
看记录是判无解和无限个解的地方出错,但是蒟蒻不知道哪里有问题,求助/bx
#include<bits/stdc++.h>
// #define LOCAL
#define sf scanf
#define pf printf
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
typedef long long ll;
constexpr int N=55;
double mp[N][N];
double ans[N];
constexpr double base=1e-7;
int n;
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("my.out","w",stdout);
#endif
sf("%d",&n);
rep(i,1,n) rep(j,1,n+1) sf("%lf",&mp[i][j]);
int mx=1;
rep(i,1,n) {
int r=mx;
rep(j,mx+1,n) if(fabs(mp[r][i])<fabs(mp[j][i])) r=j;
if(fabs(mp[r][i])<base) continue;
if(mx!=r) swap(mp[mx],mp[r]);
double div=mp[mx][i];
rep(j,i,n+1) mp[mx][j]/=div;
rep(j,mx+1,n) {
div=mp[j][i];
rep(k,i,n+1) mp[j][k]-=mp[mx][k]*div;
}
mx++;
}
if(mx<=n) {
rep(i,mx,n) if(mp[i][n+1]>base) { puts("-1"); return 0; }
puts("0"); return 0;
}
ans[n]=mp[n][n+1];
per(i,n-1,1) {
ans[i]=mp[i][n+1];
rep(j,i+1,n) ans[i]-=mp[i][j]*ans[j];
}
rep(i,1,n) pf("x%d=%.2lf\n",i,ans[i]);
}
by codwarm @ 2024-10-23 21:22:29
if(mp[i][n+1]>base) { puts("-1"); return 0; }
改为:
if(abs(mp[i][n+1])>base) { puts("-1"); return 0; }
by Hagasei @ 2024-10-23 21:24:11
36 行比较没加 fabs。这种建议写成函数。
by liyixin0514 @ 2024-10-23 21:44:58
@codwarm @Hagasei 感谢/thx 此帖结