📄 fangchengzu.cpp
字号:
/* a11X+a12Y=b1
a21X+a22Y=b2
input a11 a12 b1
a21 a22 b2*/
#define N 2
#include<iostream>
using namespace std;
void main(){
double A[N+1][N+2];
double X[N+1];
cout<<"please input the coefficient matrix:";
for(int i=1;i<=N;i++){
X[i]=0;
for(int j=1;j<=N+1;j++)
cin>>A[i][j];
}
//////////////////////////////////////////////convert the matrix to uptriangle
for(int k=1;k<=N;k++)
for(int i=k+1;i<=N;i++){
double temp;
if(A[i][k]!=0){
temp=A[k][k]/A[i][k];
for(int j=k;j<=N+1;j++){
A[i][j]=A[i][j]*temp-A[k][j];
}
}
else continue;
}
/////////////////////////////////////////////get the answer
for(int l=N;l>=1;l--){
if(A[l][l]!=0){
int k=N;
while(k>l){
A[l][N+1]=A[l][N+1]-A[l][k]*X[k];
k--;
}
X[l]=A[l][N+1]/A[l][l];
}
else return;
}
////////////////////////////////////////////
for(int m=1;m<=N;m++)
cout<<"the"<<m<<"root is:"<<X[m]<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -