WA100pts,WA#11

P2455 [SDOI2006] 线性方程组

Special_Tony @ 2024-03-12 14:53:17

rt,0->-1

# include <bits/stdc++.h>

# define ffor(i,name) \
    for (auto i = name.begin (); i != name.end (); ++ i)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

const double eps = 1e-7;

int n, sum, now = 1;

double a[105][105], mul;

int main () {

//  ios::sync_with_stdio (0);
//
//  cin.tie (0);
//
//  cout.tie (0);

    cin >> n;

    ++ n;

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

    for (int i = 1; i < n; ++ i) {

        if (fabs (a[now][i]) <= eps)
            for (int j = now + 1; j < n; ++ j)
                if (fabs (a[j][now]) > eps) {

                    swap (a[now], a[j]);

                    break ;

                }

        if (fabs (a[now][i]) <= eps)
            continue ;

        for (int j = 1; j < n; ++ j)
            if (j != now) {

                mul = a[j][i] / a[now][i];

                for (int k = i + 1; k <= n; ++ k)
                    a[j][k] -= mul * a[now][k];

            }

        ++ now;

    }

    if (now >= n)
        for (int i = 1; i < n; ++ i)
            printf ("x%d=%.2lf\n", i, a[i][n] / a[i][i]);
    else {

        for (int i = now; i < n; ++ i)
            if (fabs (a[i][n]) > eps){

                puts ("-1");

                return 0;

            }

        putchar ('0');

    }

    return 0;

}
/*
3
1 1 3 4
2 2 3 1
2 1 1 1
*/

by Special_Tony @ 2024-03-13 12:50:34

和@chenweizhen 错的一样


by Special_Tony @ 2024-03-13 12:51:50

难道被卡精度了??


by Special_Tony @ 2024-03-13 13:18:31

upd:高精度还WA

# include <bits/stdc++.h>

# define ffor(i,name) \
    for (auto i = name.begin (); i != name.end (); ++ i)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

const double eps = 1e-7;

int n, sum, now = 1, maxi;

double a[105][105], mul;

int main () {

//  ios::sync_with_stdio (0);
//
//  cin.tie (0);
//
//  cout.tie (0);

    cin >> n;

    ++ n;

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

    for (int i = 1; i < n; ++ i) {

        maxi = now;

        for (int j = now + 1; j < n; ++ j)
            if (fabs (a[j][now]) > fabs (a[maxi][now]))
                maxi = j;

        if (fabs (a[maxi][i]) <= eps)
            continue ;

        swap (a[maxi], a[now]);

        for (int j = 1; j < n; ++ j)
            if (j != now) {

                mul = a[j][i] / a[now][i];

                for (int k = i + 1; k <= n; ++ k)
                    a[j][k] -= mul * a[now][k];

            }

        ++ now;

    }

    if (now >= n)
        for (int i = 1; i < n; ++ i)
            printf ("x%d=%.2lf\n", i, a[i][n] / a[i][i]);
    else {

        for (int i = now; i < n; ++ i)
            if (fabs (a[i][n]) > eps) {

                puts ("-1");

                return 0;

            }

        putchar ('0');

    }

    return 0;

}
/*
3
1 1 3 4
2 2 3 1
2 1 1 1
*/

by WoodReal12 @ 2024-03-13 17:34:00

@Special_Tony 我测的你的代码……


by Special_Tony @ 2024-03-13 21:06:11

@chenweizhen 6,那您最后咋改AC的


by Special_Tony @ 2024-03-13 21:13:07

也和@lanjiarui 一样


by WoodReal12 @ 2024-03-14 16:52:08

@Special_Tony 我用的原来AC的


|