我太强了 @ 2019-08-23 00:27:41
大佬们快来看看qwq
#include<bits/stdc++.h>
using namespace std;
int n,flag;
double ans[105];
struct matrix
{
double z[105][105];
matrix()
{
memset(z,0,sizeof(z));
}
}a;
int gauss()
{
for(int i=1;i<=n;i++)
{
if(a.z[i][i]==0)
for(int j=i+1;j<=n;j++) if(a.z[j][i]!=0)
{
for(int k=1;k<=n+1;k++) swap(a.z[i][k],a.z[j][k]);
break;
}
if(!a.z[i][i])
{
for(int j=1;j<=n;j++) if(a.z[j][n+1]!=0) return -1;
return 0;
}
for(int j=i+1;j<=n;j++)
{
double mul=a.z[j][i]/a.z[i][i];
for(int k=1;k<=n+1;k++) a.z[j][k]-=a.z[i][k]*mul;
}
}
return 2;
}
void work()
{
ans[n]=a.z[n][n+1]/a.z[n][n];
for(int i=n-1;i>=1;i--)
{
for(int j=n;j>=i-1;j--) a.z[i][n+1]-=a.z[i][j]*ans[j];
ans[i]=a.z[i][n+1]/a.z[i][i];
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++) scanf("%lf",&a.z[i][j]);
flag=gauss();
if(flag==0||flag==-1) printf("%d\n",flag);
else
{
work();
for(int i=1;i<=n;i++)
if(fabs(ans[i])>1e-8) printf("x%d=%.2lf\n",i,ans[i]);
else printf("x%d=0\n",i);
}
return 0;
}
by zyywzw @ 2019-08-23 00:56:46
qndgxgsxy
by wudiss8 @ 2019-08-23 07:29:33
@您太强了 awa,我是WA了#3#5#6#9#10
by wudiss8 @ 2019-08-23 07:30:20
@您太强了 我的貌似是对于无解和多解的判断出来点问题(然鹅我并不了解什么情况会无解和多解)
by wudiss8 @ 2019-08-23 07:40:17
@您太强了
by wudiss8 @ 2019-08-23 07:40:59
您的提交记录显示您的程序似乎对于无穷多解的判断出了一些问题
by ShineEternal @ 2019-08-23 07:43:22
@您太强了 或许这篇文章能给你一些帮助?
by 未见堇开 @ 2019-08-23 08:01:11
精度问题,注意无解判定的精度。
by 未见堇开 @ 2019-08-23 08:01:22
@您太强了
by 我太强了 @ 2019-08-23 09:53:27
@Redspirit 是戴佬!%%%您太强了 !!
by 我太强了 @ 2019-08-23 10:19:30
@Redspirit 信你个鬼qwq,把0全替换了,#4#8依旧WA
#include<bits/stdc++.h>
using namespace std;
int n,flag;
double ans[105];
struct matrix
{
double z[105][105];
matrix()
{
memset(z,0,sizeof(z));
}
}a;
void gauss()
{
for(int i=1;i<=n;i++)
{
if(fabs(a.z[i][i])<1e-8)
for(int j=i+1;j<=n;j++) if(fabs(a.z[j][i])>1e-8)
{
for(int k=1;k<=n+1;k++) swap(a.z[i][k],a.z[j][k]);
break;
}
for(int j=i+1;j<=n;j++)
{
double mul=a.z[j][i]/a.z[i][i];
for(int k=1;k<=n+1;k++) a.z[j][k]-=a.z[i][k]*mul;
}
}
}
int check()
{
int now=2;
for(int i=1;i<=n;i++)
{
if(fabs(a.z[i][i])<1e-8&&fabs(a.z[i][n+1])<1e-8) now=0;
else if(fabs(a.z[i][i])<1e-8&&fabs(a.z[i][n+1])>1e-8) return -1;
}
return now;
}
void work()
{
ans[n]=a.z[n][n+1]/a.z[n][n];
for(int i=n-1;i>=1;i--)
{
for(int j=n;j>=i-1;j--) a.z[i][n+1]-=a.z[i][j]*ans[j];
ans[i]=a.z[i][n+1]/a.z[i][i];
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++) scanf("%lf",&a.z[i][j]);
gauss();
flag=check();
if(flag==0||flag==-1) printf("%d\n",flag);
else
{
work();
for(int i=1;i<=n;i++)
if(fabs(ans[i])>1e-8) printf("x%d=%.2lf\n",i,ans[i]);
else printf("x%d=0\n",i);
}
return 0;
}