高斯消元模板——浮点参数型

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned long long ULL;
typedef unsigned int Uint;

const int INT_INF=0x3fffffff;
const LL LL_INF=0x3fffffffffffffff;
const DB EPS=1e-9;
const DB PI=3.14159265358979323846;
const int N=10010;
const int E=10010;

#define PB push_back
#define MP make_pair
#define MH make_heap
#define PH push

DB A[N][N];
DB X[N];
bool Free[N];
int equation, variable;

void debug()
{
    printf("The Matrix is :\n");
    for(int i=0; i<=variable+1; i++)
    {
        printf("%8d",i);
        if(i==0) printf("|");
    }
    printf("\n");
    for(int i=0; i<=variable+1; i++)
    {
        printf("-------");
        if(i==0) printf("-+");
        else printf("-");
    }
    printf("\n");

    for(int i=1; i<=equation; i++)
    {
        printf("%8d|",i);
        for(int j=1; j<=variable+1; j++)
        {
            printf("%8.2lf",A[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

/***************************************************************************/
/*    功能说明:
/*    1、不需带入参数,参数已经在 build() 函数中设置好了
/*    2、函数返回 -1 :无解
/*    3、函数返回 0  :有且仅有一组解
/*    4、函数返回 其他正数 :有多解,返回的值是不确定变元的个数
/**************************************************************************/
int gauss()
{
    int col=1, row=1;
    for(; row<=equation && col<=variable; row++, col++)
    {
        int max_row=row;

        //找到 col 那列元素绝对值最大的那行与当前行交换,减小精度误差
        for(int i=row+1; i<=equation; i++)
            if(abs(A[i][col])>abs(A[max_row][col]))
                max_row=i;

        //如果 col 那列元素最大是 0,表明这一列全部是 0,处理下一列
        if(abs(A[max_row][col])<=EPS)
        {
            row--;
            continue;
        }

        //如果不是同一行,交换元素
        if(max_row!=row)
        {
            for(int i=col; i<=variable+1; i++)
                swap(A[max_row][i], A[row][i]);
        }

        //枚举要删去的行
        //for(int i=row+1; i<=equation; i++)
        for(int i=1; i<=equation; i++)
        {
            if(i==row) continue;
            if(abs(A[i][col])<=EPS) continue;

            DB ta=A[row][col];
            DB tb=A[i][col];

            for(int j=1; j<=variable+1; j++)
                A[i][j]=A[i][j]*ta-A[row][j]*tb;
        }
//        printf("row=%d col=%d\n", row, col);
//        debug();
    }

//    debug();

    // 1、没有解的情况:
    for(int i=row; i<=equation; i++)
        if(A[i][variable+1]!=0) return -1; //表明无解

    // 2、无穷解的情况:
    if(row<variable+1)
    {
        for(int i=row-1; i>=1; i--)
        {
            int free_x_num=0, free_index=-1;
            for(int j=1; j<=variable; j++)
                if(Free[j] && A[i][j]!=0)
                {
                    free_x_num++;
                    free_index=j;
                }
            if(free_x_num>1) continue;

            Free[free_index]=false;

            DB temp=A[i][variable+1];
            for(int j=1; j<=variable; j++)
                if(!(abs(A[i][j])<=EPS) && j!=free_index)
                    temp-=A[i][j]*X[j];
            X[free_index]=temp/A[i][free_index];
        }
        return variable-row+1;
    }

    // 3、唯一解的情况:
    for(int i=variable; i>=1; i--)
    {
        DB temp=A[i][variable+1];
        for(int j=i+1; j<=variable; j++)
            if(!(abs(A[i][j])<=EPS))
                temp-=A[i][j]*X[j];
        X[i]=temp/A[i][i];
    }
    return 0;
}

void build(int n, int m)
{
    memset(Free, true, sizeof(Free));
    memset(X, 0, sizeof(X));
    variable=n, equation=m;
    for(int i=1; i<=equation; i++)
        for(int j=1; j<=variable+1; j++)
            scanf("%lf",&A[i][j]);
    debug();
}

void PRINT(int flag)
{
    if(flag==-1)
    {
        printf("NO Solution !\n");
        return;
    }
    if(flag==0)
    {
        for(int i=1; i<=variable; i++)
            printf("x%d : %4.4lf\n", i, X[i]);
        return;
    }
    printf("There are %d variables who are indeterminate !\n",flag);
    for(int i=1; i<=variable; i++)
    {
        printf("x%d : ", i);
        if(Free[i]) printf("indeterminate !\n");
        else printf("%4.4lf\n",X[i]);
    }
}

int main()
{
    freopen("C:/Users/zhj5chengfeng/Desktop/in.txt","r",stdin);
    freopen("C:/Users/zhj5chengfeng/Desktop/out.txt","w",stdout);
    int ca=0;
    while(~scanf("%d%d", &variable, &equation))
    {
        printf("Case #%d :\n", ++ca);
        build(variable, equation);
        PRINT(gauss());
        printf("\n\n\n\n");
    }
    return 0;
}

/*
Input:

4 3
1 -2 1 5 3
1 -2 4 -34 0
2 -4 3 -3 5

2 2
1 2 27
2 1 0

3 3
3 -1 5 3
1 -1 2 1
1 -2 -1 2

Output:

Case #1 :
The Matrix is :
       0|       1       2       3       4       5
--------+----------------------------------------
       1|    1.00   -2.00    1.00    5.00    3.00
       2|    1.00   -2.00    4.00  -34.00    0.00
       3|    2.00   -4.00    3.00   -3.00    5.00

There are 2 variables who are indeterminate !
x1 : indeterminate !
x2 : indeterminate !
x3 : indeterminate !
x4 : indeterminate !




Case #2 :
The Matrix is :
       0|       1       2       3
--------+------------------------
       1|    1.00    2.00   27.00
       2|    2.00    1.00    0.00

x1 : -9.0000
x2 : 18.0000




Case #3 :
The Matrix is :
       0|       1       2       3       4
--------+--------------------------------
       1|    3.00   -1.00    5.00    3.00
       2|    1.00   -1.00    2.00    1.00
       3|    1.00   -2.00   -1.00    2.00

x1 : 1.4286
x2 : -0.1429
x3 : -0.2857






*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值