高斯消元法,求调
  • 板块学术版
  • 楼主limingze0509
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/12/28 17:06
  • 上次更新2024/12/28 17:11:52
查看原帖
高斯消元法,求调
643199
limingze0509楼主2024/12/28 17:06

代码如下:

#include<bits/stdc++.h>
#define int long long
#define double long double
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define Rep(i,l,r) for(int i=(l);i>=(r);--i)
#define INF 1e18
#define gc getchar()
using namespace std;
namespace fastIO{
	inline int read(){
		int f=1,x=0;char ch=gc;
		while(ch<'0'||ch>'9'){
			if(ch=='-') f=-1;
			ch=gc;
		}
		while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=gc;
		return f*x;
	}
}
using namespace fastIO;
double a[109][109],b[109],x[109];
int n;
void solve(){
	rep(i,1,n){
		int r=i;
		rep(j,i+1,n) if(fabs(a[j][i])>fabs(a[r][i])) r=j;
		swap(a[i],a[r]);
		rep(j,1,n){
			if(j==i) continue;
			double fct=a[j][i]/a[i][i];
			a[j][i]=0;
			rep(k,i+1,n){
				a[j][k]-=fct*a[i][k];
			}
			b[j]-=fct*b[i];
		}
	}
	rep(i,1,n){
		x[i]=b[i]/a[i][i];
	}
}
signed main(){
//	freopen("XXX.in","r",stdin);
//	freopen("XXX.out","w",stdout);
	cin>>n;
	rep(i,1,n){
		rep(j,1,n){
			cin>>a[i][j];
		}
		cin>>b[i];
	}
	solve();
	rep(i,1,n){
		cout<<fixed<<setprecision(2)<<x[i]<<" ";
	}
	return 0;
}
2024/12/28 17:06
加载中...