高斯消元——重载运算符的运用

谁是鸽王的题解:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int qr(void){
	char c=getchar();
	int x=0,q=1;
	while(c<48||c>57)
		q=c==45?-1:q,c=getchar();
	while(c>=48&&c<=57)
		x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return q*x;
}
#define RP(t,a,b)	for(int t=(a),edd=(b);t<=edd;t++)
typedef double db;
const double EPS=1e-20;//这是坑点,一点要小一点,这个EPS。 
int n;
const int maxn=105;
int ans[maxn];
inline db fabs(double x){
	return x>=0?x:-x;
}
struct node{
	db dat[maxn];
	double& operator [](const int &x){
		return dat[x];//重载运算符,返回引用 , 就算有dat有更多维,这样就好了,原理有关c++的“地址”系统 
	}
	node operator *(const db &x){
		node ans=(*this);//this是个指针,指向运算符左边的地址 
		for(int t=1;t<=n+1;t++)
			ans[t]*=x;
		return ans;
	}
	node operator /(const db &x){
		node ans=(*this);
		for(int t=1;t<=n+1;t++)
			ans[t]/=x;
		return ans;	
	}
	node operator -(node &x){
		node ans=(*this);
		for(int t=1;t<=n+1;t++)
			ans[t]-=x[t];
		return ans;
	}
	node operator *=(const db &x){
		return (*this)=(*this)*x;
	}
	node operator /=(const db &x){
		return (*this)=(*this)/x;
	}
	node operator -=( node &x){
		return (*this)=(*this)-x;
	}
}data[maxn];
bool vis[maxn];
inline int big(int x){
	db ans=0;
	int ret;
	for(int t=1;t<=n;t++)
		if(!vis[t]&&ans<fabs(data[t][x]))
			ret=t;
	vis[ret]=1;//根据数学原理,不可重复选择一个方程来消元 
	return ret;//为了避免乘一个过小的数字,选择一个对于该未知数绝对值最大的系数 
}
inline void kkk(void){
	RP(t0,1,n){
		int sttd=big(t0);
		const db a=data[sttd][t0];
		RP(t,1,n)
			if(t!=sttd){
				if(fabs(data[t][t0])<EPS){
					cout<<"No Solution";//防止除0 
					return;
				}
				data[t]*=(a/data[t][t0]),data[t]-=data[sttd];//将选定x0的系数和基准方程变为一致,在通过加减消元消掉,
															 //此后该未知数的系数就是0,不会再产生影响 
			}
		ans[t0]=sttd;//记录结果是哪个方程得出的 
	}
	RP(t,1,n)
		if(fabs(data[ans[t]][t])<EPS){
			cout<<"No Solution"<<endl;
			return;
		}
	RP(t,1,n){
		printf("%.2lf\n",(data[ans[t]][n+1]/data[ans[t]][t]));
	}
	return;
}
int main(){
	n=qr();
	RP(t,1,n)
		RP(i,1,n+1)
			data[t][i]=qr();
	kkk();
	return 0;//功德圆满 
}

被重载运算符摁在地上摩擦qwq

#include<bits/stdc++.h>
using namespace std;
#define MM 1e-16
int n,ans[110];
bool vis[110];
double fabs(double aa)
{ return aa>=0?aa:-aa; }
struct node
{
	double lyxy[110];
	double& operator [](const int &x){ return lyxy[x]; }
	node operator *(const double &x)
	{
		node ans=(*this);
		for(int i=1;i<=n+1;i++) ans[i]*=x;
		return ans;
	}
	node operator /(const double &x)
	{
		node ans=(*this);
		for(int i=1;i<=n+1;i++) ans[i]/=x;
		return ans;
	}
	node operator -(node &x)
	{
		node ans=(*this);
		for(int i=1;i<=n+1;i++) ans[i]-=x[i];
		return ans;
	}
	node operator *=(const double &x)
	{ return (*this)=(*this)*x; }
	node operator /=(const double &x)
	{ return (*this)=(*this)/x; }
	node operator -=(node &x)
	{ return (*this)=(*this)-x; }
}a[110];
int calc(int x)
{
	double ans=0;
	int flag;
	for(int i=1;i<=n;i++) if(!vis[i]&&ans<fabs(a[i][x])) flag=i;
	vis[flag]=1;
	return flag;
}
void work()
{
	for(int i=1;i<=n;i++)
	{
		int x=calc(i);
		double y=a[x][i];
		for(int j=1;j<=n;j++)
		{
			if(j!=x)
			{
				if(fabs(a[j][i])<MM)
				{
					printf("No Solution");
					return;
				}
				a[j]*=(y/a[j][i]);
				a[j]-=a[x];
			}
		}
		ans[i]=x;
	}
	for(int i=1;i<=n;i++) if(fabs(a[ans[i]][i])<MM)
	{
		printf("No Solution");
		return;
	}
	for(int i=1;i<=n;i++) printf("%.2lf\n",a[ans[i]][n+1]/a[ans[i]][i]);
	return;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n+1;j++) scanf("%lf",&a[i][j]);
	work();
	return 0;
}

正常版:

#include<bits/stdc++.h>
using namespace std;
double a[110][110];
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n+1;j++) scanf("%lf",&a[i][j]);
	for(int i=1;i<=n;i++)
	{
		int maxx=i;
		for(int j=i+1;j<=n;j++) if(fabs(a[j][i])>fabs(a[maxx][i])) maxx=j;
		for(int j=1;j<=n+1;j++) swap(a[i][j],a[maxx][j]);
		if(!a[i][i])
		{
			puts("No Solution");
			return 0;
		}
		for(int j=1;j<=n;j++)
		if(j!=i)
		{
			double tmp=a[j][i]/a[i][i];
			for(int k=i+1;k<=n+1;k++) a[j][k]-=a[i][k]*tmp;
		}
	}
	for(int i=1;i<=n;i++) printf("%.2lf\n",a[i][n+1]/a[i][i]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值