高斯消元 (基本线代知识)

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const int N = 15;

int n;
double a[N][N] , b[N][N];

void gauss()
{
    
    
    for(int r = 1 , c = 1 ; c <= n ; c++)
    {
    
    
        int t = r;
        for(int i = r + 1 ; i <= n ; i++)  
            if(fabs(b[t][c]) < fabs(b[i][c])) t = i;

        for(int i = c ; i <= n + 1 ; i++) swap(b[t][i] , b[r][i]);
        for(int i = n + 1 ; i >= c ; i--) b[r][i] /= b[r][c];

        for(int i = r + 1 ; i <= n ; i++)
            for(int j = c + 1 ; j <= n + 1 ; j++)
                b[i][j] -= b[i][c] * b[r][j];

        r++;
    }

    for(int i = n ; i ; i--)//求第i个未知数的解b[i][n + 1]
        for(int j = i + 1  ; j <= n ;j++)//第j个未知数的解b[j][n + 1]
            b[i][n + 1] -= b[i][j] * b[j][n + 1];
	/************** 多理解理解 **** */
	
}

int main()
{
    
    
    cin >> n;

    for(int i = 1 ; i <= n + 1 ; i++)
        for(int j = 1 ; j <= n ; j++)   
            cin >> a[i][j];

    for(int i = 1 ; i <= n ; i++)
        for(int j = 1 ; j <= n ; j++)
        {
    
    
            b[i][j] = 2 * (a[i + 1][j] - a[1][j]);
            b[i][n + 1] += a[i + 1][j] * a[i + 1][j] - a[1][j] * a[1][j];
        }   

    gauss();

    for(int i = 1 ; i <= n ; i++)   printf("%.3lf " , b[i][n + 1]);
    return 0;
}
//https://www.acwing.com/activity/content/code/content/193255/

01类:

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 11092019;
const int N = 35;

int a[N][N];
int n;

inline int Gauss()
{
    
    
    int r, c;
    for (r = 1, c = 1; c <= n; c++) {
    
    

        int t = r;
        for (int i = r + 1; i <= n; i++) {
    
    
            if (a[i][c]) {
    
    
                t = i;
                break;
            }
        }

        if (a[t][c] == 0) continue;

        for (int i = c; i <= n + 1; i++) swap(a[r][i], a[t][i]);

        for (int i = r + 1; i <= n; i++) {
    
    
            for (int j = n + 1; j >= c; j--) {
    
    
                a[i][j] ^=  a[i][c] & a[r][j];
            }
        }
        
        r++;
    }

    int res = 1;
    if (r < n + 1) {
    
    
        for (int i = r; i <= n; i++) {
    
    
            if (a[i][n + 1]) return -1;
            res *= 2;
        }
    }
    
    return res;
}

inline void Init()
{
    
    
    memset(a, 0, sizeof(a));
}

int main()
{
    
    
    //ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int T; cin >> T;
    while (T--) {
    
    

        Init();

        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i][n + 1];
        for (int i = 1; i <= n; i++) {
    
    
            int t; cin >> t;

            a[i][n + 1] ^= t;
            a[i][i] = 1;
        }

        int i, j;
        while (cin >> i >> j , i && j) {
    
    
            a[j][i] = 1;
        }

        int ans = Gauss();

        if (ans == -1) puts("Oh,it's impossible~!!");
        else cout << ans << endl;
    }

    return  0;
}
//https://www.acwing.com/activity/content/problem/content/1776/1/

Guess you like

Origin blog.csdn.net/YingShen_xyz/article/details/113918517