Φρανκ @ 2022-11-09 08:40:31
#include <bits/stdc++.h>
using namespace std;
int n, t;
double a[51][52], res;
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n+1; j++)
cin>>a[i][j];
for(int i=1; i<=n; i++)
{
t=i;
for(int j=i; j<=n; j++)
if(abs(a[j][i])>abs(a[t][i]))
t=j;
if(a[t][i]==0)
continue;
for(int j=1; j<=n+1; j++)
swap(a[i][j], a[t][j]);
for(int j=1; j<=n; j++)
if(j!=i)
{
for(int k=1; k<=n+1; k++)
if(k!=i)
a[j][k]-=a[i][k]*a[j][i]/a[i][i];
a[j][i]=0;
}
}
// for(int i=1; i<=n; i++)
// {
// for(int j=1; j<=n+1; j++)
// cout<<a[i][j]<<" ";
// cout<<endl;
// }
for(int i=1; i<=n; i++)
if(a[i][i]==0 && int(100*a[i][n+1])!=0)
{
cout<<-1;
return 0;
}
else if(a[i][i]==0 && a[i][n+1]==0)
{
cout<<0;
return 0;
}
cout<<fixed<<setprecision(2);
for(int i=1; i<=n; i++)
if(a[i][n+1]/a[i][i]>-0.0001 && a[i][n+1]/a[i][i]<0.0001)
cout<<"x"<<i<<"="<<0.00<<endl;
else
cout<<"x"<<i<<"="<<a[i][n+1]/a[i][i]<<endl;
return 0;
}
感谢各位!
by 天空即为极限 @ 2022-11-09 09:25:55
@Φρανκ 先把无解都判完在判无穷解
by Φρανκ @ 2022-11-09 09:35:42
@天空即为极限 感谢!但经测试仍不可
by 天空即为极限 @ 2022-11-09 10:21:56
@Φρανκ
3
0 0 1 1
0 1 0 1
0 1 0 1
手模一下
by Φρανκ @ 2022-11-09 11:32:35
@天空即为极限 感谢,正在修改
by Debarkation @ 2022-11-22 18:40:02
@Φρανκ 你知道错在哪了吗我也是wa 这俩点
by Φρανκ @ 2022-11-22 22:04:27
@天空即为极限 已过,感谢!
@Debarkation 我发现是之前因为全
by 233L @ 2023-02-01 12:30:26
谢谢orz