# 高斯消去法 --- # CODE ```cpp #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #include <bits/stdc++.h> #define IO ios::sync_with_stdio(0),cin.tie(0) #define F first #define S second #define MP make_pair #define PB push_back #define PPB pop_back #define ALL(v) v.begin(),v.end() #define F0R(i,n) for(int i=0;i<=n;++i) #define FOR(i,m,n) for(int i=m;i<=n;++i) #define RFOR(i,m,n) for(int i=m;i>=n;--i) #define RF0R(i,n) for(int i=n;i>=0;--i) #define SZ(v) (int) v.size() #define maxE(v) (*max_element((x).begin(), (x).end())) #define minE(v) (*min_element((x).begin(), (x).end())) #define SETW(x) setiosflags(ios::left) << setw(x) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<double,double> PDD; typedef vector<int> VI; typedef vector<PII> VPII; typedef vector<LL> VLL; typedef vector<double> VD; typedef vector<PDD> VPDD; typedef __int128 I128; //-------------------------------------------------------- #define gcd __gcd constexpr int N=105; int n; struct FRAC{int a,b;} mat[N][N]; inline FRAC norm(const FRAC &x){ int aa=x.a,bb=x.b; int gcd_=gcd(aa,bb); aa/=gcd_; bb/=gcd_; if(bb<0) aa=-aa,bb=-bb; return {aa,bb}; } FRAC operator+(const FRAC &x,const FRAC &y){ int gcd_=gcd(x.b,y.b); int bb=x.b*y.b/gcd_; int aa=x.a*(y.b/gcd_)+y.a*(x.b/gcd_); return norm({aa,bb}); } FRAC operator-(const FRAC &x,const FRAC &y){ int gcd_=gcd(x.b,y.b); int bb=x.b*y.b/gcd_; int aa=x.a*(y.b/gcd_)-y.a*(x.b/gcd_); return norm({aa,bb}); } FRAC operator*(const FRAC &x,const FRAC &y){ int aa=x.a*y.a,bb=x.b*y.b; return norm({aa,bb}); } FRAC operator/(const FRAC &x,const FRAC &y){ FRAC y2={y.b,y.a}; return x*y2; } void eli(const int& from,const int& to){ FRAC mul_=mat[to][from]/mat[from][from]; FOR(i,from,n) mat[to][i]=mat[to][i]-(mul_*mat[from][i]); return; } void solve(){ F0R(nc,n-1){ if(mat[nc][nc].a==0) FOR(j,nc+1,n-1){ if(mat[j][nc].a!=0){ F0R(i,n) swap(mat[nc][i],mat[j][i]); break; } } if(mat[nc][nc].a==0) continue; for(int i=nc+1;i<n;i++) eli(nc,i); } return; } void print(const FRAC &x){ string s=to_string(x.a); if(x.b!=1) {s+="/";s+=to_string(x.b);} cout << SETW(10) << s; return; } signed main(){ cout << "請問此方陣之列數為何(1~100)\n"; cin >> n; cout << "請輸入其增廣矩陣,兩數字間以空格隔開\n"; F0R(i,n-1)F0R(j,n){ mat[i][j].b=1; cin >> mat[i][j].a; } solve(); cout << "---------------------------------------------\n"; cout << "此為進行完高斯消去法後的增廣矩陣:\n"; F0R(i,n-1){ F0R(j,n){ print(mat[i][j]); cout << ' '; } cout << '\n'; } cout << "---------------------------------------------\n"; bool infinite=false; F0R(i,n-1){ bool zero=true; F0R(j,n-1){ if(mat[i][j].a!=0){ zero=false; break; } } if(zero){ if(mat[i][n].a==0) infinite=true; else{ cout << "此方程無解\n"; return 0; } } } if(infinite){ cout << "此方程有無限多組解\n"; return 0; } vector<FRAC> ans(n); RF0R(i,n-1){ FRAC now=mat[i][n]; FOR(j,i+1,n-1){ now=now-(mat[i][j]*ans[j]); } ans[i]=now/mat[i][i]; } cout << "此為未知數之數值:\n"; F0R(i,n-1){ cout << 'a' << i+1 << "= "; print(ans[i]); cout << '\n'; } return 0; } ``` --- # 整數 ![螢幕擷取畫面 2025-02-09 163030](https://hackmd.io/_uploads/rkhVFyUtkg.png) --- # 分數 ![螢幕擷取畫面 2025-02-09 163050](https://hackmd.io/_uploads/HJx2VK18YJg.png) --- # 無解 ![螢幕擷取畫面 2025-02-09 163545](https://hackmd.io/_uploads/SynNt1Itye.png) --- # 無限多組解 ![螢幕擷取畫面 2025-02-09 163622](https://hackmd.io/_uploads/Hk3VtkUF1x.png)
{"title":"高斯消去法","contributors":"[{\"id\":\"b3c5fd10-313b-43e1-9849-80b434b3e8ea\",\"add\":11752,\"del\":7753}]","description":"螢幕擷取畫面 2025-02-09 161527"}
    139 views