社区讨论

判无解无限解有误求调

P2455[SDOI2006] 线性方程组参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m041bpe6
此快照首次捕获于
2024/08/21 23:55
2 年前
此快照最后确认于
2024/08/22 08:35
2 年前
查看原帖
RT,WA on #9 #11 #12 #13
CPP
#include <bits/stdc++.h>
using namespace std;
int n;
double a[105][105];
const double eps = 1e-6;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n + 1; j++)
        {
            cin >> a[i][j];
        }
    }
    cout << fixed << setprecision(2);
    for (int i = 1; i <= n; i++)
    {
        int pla = i;
        while (pla <= n && a[pla][i] == 0)
        {
            pla++;
        }
        if (pla > n)
        {
            break;
        }
        for (int j = 1; j <= n + 1; j++)
        {
            swap(a[i][j], a[pla][j]);
        }
        double tmp = a[i][i];
        for (int j = 1; j <= n + 1; j++)
        {
            a[i][j] /= tmp;
        }
        for (int j = 1; j <= n; j++)
        {
            if (i == j)
            {
                continue;
            }
            tmp = a[j][i];
            for (int k = 1; k <= n + 1; k++)
            {
                a[j][k] -= tmp * a[i][k];
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        bool a0 = true;
        for (int j = i + 1; j <= n; ++j)
            if (abs(a[i][j]) >= eps) {
                a0 = false;
                break;
            }
        if (a0 && abs(a[i][i]) <= eps && abs(a[i][n + 1]) >= eps) {
            cout << "-1\n";
            return 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        bool a0 = true;
        for (int j = i + 1; j <= n; ++j)
            if (abs(a[i][j]) >= eps) {
                a0 = false;
                break;
            }
        if (!a0) {
            cout << "0\n";
            return 0;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << 'x' << i << '=' << a[i][n + 1] << endl;
    }
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...