WA#11
查看原帖
WA#11
784704
kevin4楼主2023/11/11 22:32

rt.

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
double a[105][105];
string s,s1;
bool gauss() {
	int r=0;//useful line number
	for(int c=0; c<n; c++) { //c is the current principal element
		int t=c;
		for(int i=r; i<n; i++)t=(fabs(a[i][c])>fabs(a[t][c]))?i:t;
		if(a[t][c]==0)continue;
		for(int i=0;i<=n;i++)swap(a[r][i],a[t][i]);//swap the current line
		for(int i=n-1;i>=r;i--)for(int j=n;j>=0;j--)a[i][j]/=a[i][c];//factor the coefficient to 1
		for(int i=n-1;i>r;i--)for(int j=0;j<=n;j++)a[i][j]-=a[r][j];//elimination
		r++;
	}
	if(r!=n)return true;
	for(int i=n-1;i>=0;i--){
		for(int j=i+1;j<n;j++){
			a[i][n]-=a[i][j]*a[j][n];
		}
	}
	return false;
}
inline int read() {
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') {
		if (ch=='-') f=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9') {
		x=x*10+ch-48;
		ch=getchar();
	}
	return x*f;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	n=read();
	for(int i=0; i<n; i++) {
		for(int j=0; j<=n; j++) {
			a[i][j]=read();
		}
	}
	if(gauss())puts("No Solution");
	else for(int i=0;i<n;i++)printf("%.2lf\n",a[i][n]);
	return 0;
}
2023/11/11 22:32
加载中...