Dream_not_found @ 2024-03-24 12:17:56
整体第一遍去写的时候就去按照高斯消元的过程去模拟了一下20分WA。
跟着题解又写了一遍,60分WA
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+10,M=1e5+10;
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
int n;
int p;
double a[100][100];
int main(){
// freopen("xxx.in","r",stdin);
// freopen("xxx.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n+1;j++)scanf("%lf",&a[i][j]);
}
p=1;
for(int zhu=1;zhu<=n;zhu++){
int f=p;
for(int i=p+1;i<=n;i++){
if(fabs(a[i][zhu])>fabs(a[f][zhu]))f=i;
}
if(fabs(a[f][zhu])<eps)continue;
for(int i=1;i<=n+1;i++)swap(a[f][i],a[zhu][i]);
// for(int i=n+1;i>=1;i--)a[p][i]/=a[p][zhu];
for(int i=1;i<=n;i++){
if(i!=p){
double x=a[i][zhu]/a[p][zhu];
for(int j=zhu;j<=n+1;j++)a[i][j]-=a[p][j]*x;
}
}
++p;
}
if(p<n+1){
while(p<n+1){
if((fabs(a[p++][n+1])>eps)){
printf("-1");
return 0;
}
}
}
else{
for(int i=1;i<=n;i++){
printf("x%d=%.2lf\n",i,a[i][n+1]/a[i][i]);
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
样例输出
x1=1.00
x2=-0.00
x3=-1.00
求调
by Dream_not_found @ 2024-03-24 12:18:32
就是不知道-0.00怎么来的