首页 > 其他分享 >【板子】高斯约旦消元法(可判解)

【板子】高斯约旦消元法(可判解)

时间:2024-01-26 21:14:44浏览次数:23  
标签:mxline const int double GaussJordan 可判 板子 元法

//lg 2455
#include<bits/stdc++.h>
using namespace std;

const double eps = 0.000001;
const int N = 105;

double a[N][N];
int n;
int nowline=1;//存储当前行

void GaussJordan()
{
	for(int i=1;i<=n;i++) //枚举列 若方程有唯一解 则与枚举行列等效
	{
		int mxline=nowline;
		for(int k=i;k<=n;k++) if(fabs(a[k][i])>fabs(a[mxline][i])) mxline=k;
		if(fabs(a[mxline][i])<eps) continue;
		if(mxline!=nowline) swap(a[mxline],a[nowline]);
		for(int k=1;k<=n;k++)
		{
			if(k==nowline) continue;
			double t=a[k][i]/a[nowline][i];
			for(int j=i;j<=n+1;j++) a[k][j]-=t*a[nowline][j];
		}
		nowline++;
	}
	if(nowline==n+1)
	{
		for(int i=1;i<=n;i++) printf("x%d=%.2lf\n",i,a[i][n+1]/a[i][i]);
		return ;
	}
	while(nowline<n+1)
	{
		if(fabs(a[nowline][n+1])>eps) 
		{
			cout<<-1;
			return ;
		}
		nowline++;
	}
	cout<<0;
	return ;
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n+1;j++)
		{
			cin>>a[i][j];
		}
	}
	GaussJordan();
	return 0;
}

标签:mxline,const,int,double,GaussJordan,可判,板子,元法
From: https://www.cnblogs.com/yeyou26/p/17990718

相关文章

  • 【板子】快读/快写
    //double快读inlinevoidReadouble(double&ans){ ans=0; doubley=1.0; boolflag=0; charch=getchar(); while(!isdigit(ch)&&~ch) { flag|=(ch=='-'); ch=getchar(); } while(isdigit(ch)&&~ch) { ans=ans*10+(ch^48);......
  • 【板子】快速排序
    #include<bits/stdc++.h>usingnamespacestd;inta[114514];voidQuicksort(intl,intr);intmain(){freopen("working.in","r",stdin);freopen("working.out","w",stdout);intn;cin>>n;......
  • 【板子】归并排序
    #include<bits/stdc++.h>usingnamespacestd;constintN=1e6+6;intn;inta[N];intb[N];voidMergesort(intl,intr);longlongcnt;intmain(){freopen("working.in","r",stdin);freopen("working.out",&......
  • 【板子】树状数组(BIT)
    //lg1908求逆序对//Copyrightyeyou26#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintN=(int)1e6+6;llsum;intn;structData{intorigin;intls;intid;}data[N];boolcmporigin(Datax,Datay){r......
  • 【板子】强连通分量(SCC)
    //强连通分量//lg2863求强连通分量的数量#include<bits/stdc++.h>usingnamespacestd;constintN=(int)2e4+4;intwhere[N];//这个点在哪个scc里intscccnt;intsccsize[N];intlow[N],dfn[N],idx;boolinstk[N];stack<int>stk;vector<int>e[N];intn,m;......
  • 【板子】字符串哈希
    //lgp3370//Copyrightyeyou26#include<bits/stdc++.h>usingnamespacestd;#defineullunsignedlonglongstrings;intn;constullp=998244353;ullnow_hash;ullv[100005];intcnt;intans;voidget_hash();voiddo_compare();voidinit()......
  • 【板子】字符串最小表示法
    //lgp1368//Copyrightyeyou26#include<bits/stdc++.h>usingnamespacestd;longlonga[600005];intn;voidinit();voidsolve(){inti=1,j=2,k=0;while(i<=n&&j<=n){k=0;while(a[i+k]==a[j+k]&&am......
  • 【板子】KMP
    //lgp3375//Copyrightyeyou26#include<bits/stdc++.h>usingnamespacestd;charp[1000005],s[1000005];intlenp,lens;intlst[1000005];voidinit();voidpre_work();voidkmp();voidout_put();intmain(){freopen("working.in",&qu......
  • P3389 【模板】高斯消元法
    #include<bits/stdc++.h>usingnamespacestd;doublemax(doublea,doubleb){ if(a>=b)returna; if(a<b)returnb;}intn;doublea[1010][1010];doublea1[1010][1010];intmain(){ scanf("%d",&n); for(inti=1;i<=n;i++) { ......
  • 一些神奇の小公式&板子
    一些神奇の小公式$n$以内的质数个数为:​ $n/\logn*\sqrt{n}$$n$个点的距离平方和:​ $n*(\sumx_i+\sumy_i)-[(\sumx_i)^2+(\sumy_i)^2]$一些神奇の板子万年不变万能(火车)头#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>#inc......