当前位置:网站首页>Gauss elimination method and template code
Gauss elimination method and template code
2022-07-04 00:33:00 【Alkali!】
Gauss elimination method for solving linear equations
Ideas and steps

The last one ( The approximate ) Ladder matrix 
Then reduce it to an approximate identity matrix , We can get the solution
Template code
// Background :AcWing 883
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=110;
const double eps=1e-8; // Do not put double Misspelled int, The reason why it should be less than 1e-8, Because c++ A drawback of floating point numbers , So less than eps when , It can be approximately regarded as 0
double a[N][N]; // Storage augmentation matrix
int n;
int gauss()
{
int r,c; //r Indicates the current line to be processed
for(r=0,c=0;c<n;c++) // Traverse each column
{
int t=r;
for(int i=r;i<n;i++) // Find the row with the largest element in this column
if(fabs(a[i][c])>fabs(a[t][c]))
t=i;
if(fabs(a[t][c])<eps) continue; // If the element is the largest , still 0, Then skip , Go to the next column
for(int i=c;i<=n;i++) swap(a[t][i],a[r][i]); // Put the selected line in “ At the top ” Go to
for(int i=n;i>=c;i--) a[r][i] /=a[r][c]; // Put the first in this line c Into 1
for(int i=r+1;i<n;i++) // Put the second line c Column elimination into 0
if(fabs(a[i][c])>eps)
{
for(int j=n;j>=c;j--)
a[i][j]-=a[i][c]*a[r][j];
}
r++;
}
if(r<n) // If the final step is not strictly complete
{
for(int i=r;i<n;i++)
if(fabs(a[i][n])>eps) //0== Non zero case , unsolvable
return 2;
return 1; //0==0 The situation of , There are infinite solutions
}
for(int i=n-1;i>=0;i--) // Find the solution from bottom to top
for(int j=i+1;j<n;j++)
a[i][n]-=a[j][n]*a[i][j];
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n+1;j++)
scanf("%lf",&a[i][j]);
int t=gauss();
if(t==2)
printf("No solution");
else if(t==1)
printf("Infinite group solutions");
else
for(int i=0;i<n;i++)
{
if(fabs(a[i][n])<eps)
a[i][n]=0;
printf("%.2lf\n",a[i][n]);
}
return 0;
}
Gauss elimination method for solving XOR linear equations
Ideas and steps
It is consistent with Gauss elimination method in solving linear equations 

Template code
// Background :AcWing 884
#include<iostream>
#include<algorithm>
using namespace std;
const int N=110;
int n;
int a[N][N];
int gauss()
{
int r,c;
for(r=0,c=0;c<n;c++) // Traverse each column
{
int t=r;
for(int i=r;i<n;i++)
if(a[i][c]) // Find No c The first in the column is not 0 All right
{
t=i;
break;
}
if(!a[t][c]) continue; // If it's all 0 了 , Just continue
for(int i=c;i<=n;i++) swap(a[r][i],a[t][i]); // Put this line on top
for(int i=r+1;i<n;i++) // Delete the following line with the selected line , The first c Column elimination as 0
if(a[i][c])
for(int j=c;j<=n;j++)
a[i][j]^=a[r][j];
r++;
}
if(r<n)
{
for(int i=r;i<n;i++)
if(a[i][n])
return 2;
return 1;
}
for(int i=n-1;i>=0;i--)
for(int j=i+1;j<n;j++)
if(a[i][j])
a[i][n]^=a[j][n];
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n+1;j++)
scanf("%d",&a[i][j]);
int res=gauss();
if(res==2)
printf("No solution");
else if(res==1)
printf("Multiple sets of solutions");
else
for(int i=0;i<n;i++)
printf("%d\n",a[i][n]);
return 0;
}
边栏推荐
- [complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
- [software testing] you haven't mastered these real interview questions of big companies?
- Selenium library 4.5.0 keyword explanation (II)
- Several ways to set up a blog locally [attach relevant software download links]
- 不得不会的Oracle数据库知识点(二)
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
- A dichotomy of Valentine's Day
- Qtcharts notes (V) scatter diagram qscatterseries
- [error record] configure NDK header file path in Visual Studio
- MySQL winter vacation self-study 2022 12 (2)
猜你喜欢

MySQL winter vacation self-study 2022 12 (1)
![[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept](/img/b4/961b3b44e9ecbfd4bddd04318b663a.jpg)
[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept

2022 system integration project management engineer examination knowledge points: software development model
![[about text classification trick] things you don't know](/img/c0/fdb04f6b31f1dba2658c2430dc4036.jpg)
[about text classification trick] things you don't know

It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence

Makefile judge custom variables

What is the Valentine's Day gift given by the operator to the product?

(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

Idea a method for starting multiple instances of a service
随机推荐
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
Interview script of Software Test Engineer
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
Anomalies seen during the interview
[GNN] hard core! This paper combs the classical graph network model
Advanced C language - pointer 2 - knowledge points sorting
CSP window
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Analysis: misunderstanding of choosing WMS warehouse management system
Briefly understand the operation mode of developing NFT platform
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
[CSDN Q & A] experience and suggestions
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
MySQL winter vacation self-study 2022 12 (2)
Reading notes on how programs run
How to trade spot gold safely?