【高斯消元】

double a[maxn][maxn];
double x[maxn];
double EPS = 1e-6;
int n, m;


int Gauss()
{
    int i, j, k;
    int max_r;
    double tmp;

    for(i = 1; i <= n; i++)
    {
        max_r = i;
        for(j = i; j <= m; j++)
        {
            if( fabs(a[j][i]) > fabs(a[max_r][i]) )
                max_r = j;
        }
        if(max_r == i && fabs(a[i][i]) < EPS)
            return -1;
        if(max_r != i)
            swap(a[i], a[max_r]);

        for(j = i + 1; j <= m; j++)
        {
            tmp = a[j][i] / a[i][i];
            for(k = n + 1; k > i; k--)
                a[j][k] -= a[i][k] * tmp;
            a[j][i] = 0;
        }
    }

    for(i = n; i <= m; i++)
    {
        j = 1;
        while(j <= n && fabs(a[i][j]) < EPS) j++;
        if(j == n + 1 && fabs(a[i][j]) > EPS)
            return 0;
    }

    for(i = n; i > 0; i--)
    {
        for(j = i + 1; j <= n; j++)
            a[i][n + 1] -= a[i][j] * x[j];
        x[i] = a[i][n + 1] / a[i][i];
    }

    return 1;
}


你可能感兴趣的:(数论,-,高斯消元)