WA60pts求调

P2455 [SDOI2006] 线性方程组

川寰boy @ 2024-04-07 21:31:53

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <deque>

using namespace std;

#define rd read()
#define int long long

void write(int n)
{
    if (n < 0)
    {
        putchar('-');
        n = -n;
    }
    if (n < 10)
    {
        putchar(n + '0');
        return;
    }
    write(n / 10);
    putchar(n % 10 + '0');
    return;
}
void wt(int n, bool o = 1)
{
    write(n);
    if (!o)
    {
        putchar(' ');
    }
    else
    {
        putchar('\n');
    }
}
void debug(int n, int _1 = 0, int _2 = 0, int _3 = 0, int _4 = 0, int _5 = 0, int _6 = 0)
{
    int _[7] = {_1, _2, _3, _4, _5, _6};
    for (int i = 0; i < n; i++)
    {
        wt(_[i], 0);
    }
    putchar('\n');
}

int read()
{
    char ch;
    int type = 1, n = 0;
    ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
        {
            type = -1;
        }
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        n = (n << 1) + (n << 3) + (ch ^ 48);
        ch = getchar();
    }
    return n * type;
}

const double eps = 1e-9;
const int N = 200 + 10;
const int MOD = 998244353;
const int INF = 0x7fffffff;
const int Fill = 0x3f;

struct matrix
{
    int weight, height; // weight = qheight
    vector<vector<double>> mar;
} A;

int n;

double absf(double x)
{
    return x < 0 ? -x : x;
}

void print()
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("x%d=%.2lf\n", i + 1, A.mar[i][n]);
    }
    return;
}

int Gauss()
{
    int i, j;
    int row, col; // 行,列

    // 消元部分
    for (col = 0, row = 0; col < A.weight - 1; col++)
    {
        int maxid = row;
        for (i = row; i < A.height; i++)
        {
            if (absf(A.mar[i][col]) > absf(A.mar[maxid][col])) // 加abs
            {
                maxid = i;
            }
        }
        if (absf(A.mar[maxid][col]) < eps) // 判 0
        {
            continue; // 无解或有自由元,为了判无解所以要跑完
        }
        swap(A.mar[row], A.mar[maxid]);
        for (i = A.weight - 1; i >= col; i--)
        {
            A.mar[row][i] /= A.mar[row][col];
        }
        for (i = row + 1; i < A.height; i++)
        {
            if (fabs(A.mar[i][col]) < eps) // 判 0
            {
                continue;
            }
            for (j = A.weight - 1; j >= col; j--)
            {
                A.mar[i][j] -= A.mar[i][col] * A.mar[row][j];
            }
        }
        row++;
    }
    // 回带部分
    for (i = A.height - 1; i >= 0; i--)
    {
        for (j = i + 1; j < A.weight - 1; j++)
        {
            A.mar[i][A.weight - 1] -= A.mar[i][j] * A.mar[j][A.weight - 1];
        }
    }
    // 判断部分
    if (row < A.height)
    {
        for (i = row; i < A.height; i++)
        {
            if (fabs(A.mar[i][A.weight - 1]) > eps)
            {
                return 0; // 无解
            }
        }
        return 2; // 自由元
    }
    return 1; // 有解
}

void solve()
{
    int i, j;
    n = rd;
    vector<double> tmp;
    int x;
    A.height = n, A.weight = n + 1;
    for (i = 0; i < A.height; i++)
    {
        tmp.clear();
        for (j = 0; j < A.weight; j++)
        {
            x = rd;
            tmp.push_back(x);
        }
        A.mar.push_back(tmp);
    }
    int opt = Gauss();
    if (opt == 1)
    {
        print();
    }
    if (opt == 2)
    {
        wt(-1);
    }
    if (opt == 3)
    {
        wt(0);
    }
}
signed main()
{
    int T;
    int i, j;
    T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

by 川寰boy @ 2024-04-07 21:32:12

马蜂丑轻喷


by hellolin @ 2024-04-08 07:25:16

你太卷了!


by white_carton @ 2024-04-10 19:36:27

你太卷了!


by WilliamFranklin @ 2024-04-10 20:17:25

@川寰boy 你试试特判一下 -0.00 的情况?


by WilliamFranklin @ 2024-04-10 20:21:37

@川寰boy 就是说,你输出是判一下 A.mar[i][n] == -0.00,如果是,就直接输出 0.00 即可。


by WilliamFranklin @ 2024-04-10 20:28:03

是 -> 时


by 川寰boy @ 2024-04-10 21:48:56

@WilliamFranklin ?啊?

为什么会有-0.00 的情况


by WilliamFranklin @ 2024-04-10 22:55:53

@川寰boy 应该是因为你这个答案非常小,比如 -0.00000000001


|