Dreamweaver @ 2021-10-13 21:31:04
WA #4 #8
不太会判断无解和无限解,求助qwq
#include<iostream>
#include<cstdio>
#include<cmath>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define maxn 110
#define inf 0x7f7f7f7f
#define mod 1000000007
#define esp 1e-6
#define re register
inline int read();
inline void wn(int x);
inline void wr(int x);
inline void wi(int x);
double a[maxn][maxn];
double ans[maxn];
int n;
signed main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
// printf("%d M\n",((sizeof(z))>>20));
cin>>n;
for(re int i=1;i<=n;++i)
for(re int j=1;j<=n+1;++j)
scanf("%lf",&a[i][j]);
for(re int i=1;i<=n;++i)
{
int m=i;
for(re int j=i+1;j<=n;++j)
if(fabs(a[j][i])>fabs(a[m][i])) m=j;
if(fabs(a[m][i])<esp) continue;
if(m!=i) swap(a[m],a[i]);
double d=a[i][i];
for(re int j=i;j<=n+1;++j)
a[i][j]/=d;
for(re int j=i+1;j<=n;++j)
{
d=a[j][i];
for(re int k=i;k<=n+1;++k)
{
a[j][k]-=a[i][k]*d;
}
}
}
//for(re int i=1;i<=n;++i,puts(""))
// for(re int j=1;j<=n+1;++j)
// printf("%.2lf ",a[i][j]);
bool p1=false,p2=false;
for(re int i=1;i<=n;++i)
if(fabs(a[i][i])<esp&&fabs(a[i][n+1])>esp)
{
p1=true;
break;
}
if(p1)
{
puts("-1");
return 0;
}
for(re int i=1;i<=n;++i)
if(fabs(a[i][i])<esp&&fabs(a[i][n+1])<esp)
{
p2=true;
break;
}
if(p2)
{
wr(0);
return 0;
}
ans[n]=a[n][n+1];
for(re int i=n-1;i>=1;--i)
{
ans[i]=a[i][n+1];
for(re int j=i+1;j<=n;++j)
ans[i]-=a[i][j]*ans[j];
}
for(re int i=1;i<=n;++i,puts(""))
(fabs(ans[i])<esp)?printf("x%d=%.2lf",i,0.00):printf("x%d=%.2lf",i,ans[i]);
return 0;
}
//==========================================================
inline int read()
{
int f=1,x=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x*f;
}
inline void wn(int x)
{
if(x<0)
{
putchar('-');
wn(-x);
return ;
}
if(x>=10) wn(x/10);
putchar('0'+x%10);
}
inline void wr(int x)
{
wn(x);
putchar('\n');
}
inline void wi(int x)
{
wn(x);
putchar(' ');
}