90 pts 求助 WA on #9 #13

P2455 [SDOI2006] 线性方程组

Grow2011 @ 2024-07-11 09:12:17

代码是这样的:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,x[105][105];
long double a[105][105];
long double ans[105];
signed main(){
    cin >> n;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)cin >> x[i][j];
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)a[i][j] = x[i][j]*1.0;
    }
    for(int i = 1;i<n;i++){
        for(int j = 1;j<=n-i;j++){
            if(a[j][n-i+1]==0)continue;
            double x = a[j+1][n-i+1]/a[j][n-i+1];
            if(x==0){
                for(int k = 1;k<=n+1;k++)swap(a[j][k],a[j+1][k]);
                continue;
            }
            for(int k = 1;k<=n+1;k++)a[j][k]*=x;
            for(int k = 1;k<=n+1;k++)a[j][k]-=a[j+1][k];
        }
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(a[i][j]!=0)fp = 0;
        if(fp){
            if(a[i][n+1]!=0)cout << "-1";
            else cout << "0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(a[j][i]!=0)fp = 0;
        if(fp){
            if(a[i][n+1]!=0)cout << "-1";
            else cout << "0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<i;j++){
            a[i][n+1]-=ans[j]*a[i][j];
        }
        ans[i] = a[i][n+1]/a[i][i];
    }
    for(int i = 1;i<=n;i++)if(ans[i]==-0)ans[i] = 0;
    for(int i = 1;i<=n;i++)printf("x%d=%.2Lf\n",i,ans[i]);
    return 0;
}

by Grow2011 @ 2024-07-11 09:29:12

现在 Unaccepted 100:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,x[105][105];
long double a[105][105];
long double ans[105];
long double zero = 1e-6;
signed main(){
    cin >> n;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)cin >> x[i][j];
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)a[i][j] = x[i][j]*1.0;
    }
    for(int i = 1;i<n;i++){
        for(int j = 1;j<=n-i;j++){
            if(fabs(a[j][n-i+1])<=zero)continue;
            long double x = a[j+1][n-i+1]/a[j][n-i+1];
            if(fabs(x)<=zero){
                for(int k = 1;k<=n+1;k++)swap(a[j][k],a[j+1][k]);
                continue;
            }
            for(int k = 1;k<=n+1;k++)a[j][k]*=x;
            for(int k = 1;k<=n+1;k++)a[j][k]-=a[j+1][k];
        }
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(a[i][j]!=0)fp = 0;
        if(fp){
            if(fabs(a[i][n+1])>zero)cout << "-1";
            else cout << "0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(fabs(a[j][i])>zero)fp = 0;
        if(fp){
            if(a[i][n+1]!=0)cout << "-1";
            else cout<<"0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<i;j++){
            a[i][n+1]-=ans[j]*a[i][j];
        }
        ans[i] = a[i][n+1]/a[i][i];
    }
    for(int i = 1;i<=n;i++)if(fabs(ans[i])<=zero)ans[i] = 0;
    for(int i = 1;i<=n;i++)printf("x%lld=%.2Lf\n",i,ans[i]);
    return 0;
}

by endswitch @ 2024-07-11 09:42:51

@Grow2011 借楼求条 90 pts WA on #9 #12 #13

#include<bits/stdc++.h>
using namespace std;
constexpr int N = 1e2 + 5;
const double eps = 1e-6;

inline int read(){
    register char ch = getchar();
    register int x = 0;
    register bool f = 0;
    while(!isdigit(ch)) f |= (ch == '-') , ch = getchar();
    while(isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48) , ch = getchar();
    return f ? -x : x;
}

int n;

double a[N][N];

inline int Gauss(){
    bool flag = 0;

    int nowline = 1;

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

        for(register int j = i ; j <= n ; ++ j)
            if(fabs(a[j][i]) > eps) {
                swap(a[i] , a[j]);
                break;
            }

        if(fabs(a[i][i]) < eps) {
            flag = 1;
            continue;
        }

        //for(register int j = 1 ; j <= n + 1 ; ++ j) swap(a[newline][j] , a[nowline][j]);
        for(register int j = n + 1; j >= i ; -- j) a[i][j] /= a[i][i];

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

        //++ nowline;
    }

    if(flag) {
        for(register int i = 1 ; i <= n ; ++ i){
            bool yg = 0;
            for(register int j = 1 ; j <= n ; ++ j) {
                if(a[i][j] != 0) {
                    yg = 1;
                    break;
                }
            }
            if(yg) continue;

            if(fabs(a[i][n + 1]) > eps) return -1;      
        }       

        return 0;
    }

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

    return 1;
}

int main(){
    n = read();

    for(register int i = 1 ; i <= n ; ++ i)
        for(register int j = 1 ; j <= n + 1 ; ++ j)
            a[i][j] = read();

    int backin = Gauss();

    if(backin == 1)
        for(register int i = 1 ; i <= n ; ++ i)
            printf("x%d=%.2f\n" , i , a[i][n + 1]);

    else printf("%d" , backin); 
    return 0;
}

by zhang_kevin @ 2024-07-23 13:07:42

@Grow2011 来自 12 天后的回复......

您需要考虑到这种情况:

3
0 0 0 0
0 0 1 2
0 0 5 11

答案为 -1,但代码的输出是 0

您可以使用这段代码进行特判:

for(int i = 1; i <= n; i++){
    bool flag = true;
    for(int j = 1; j <= n; j++) if(a[i][j]) flag = false;
    if(flag && a[i][n+1]) puts("-1"), exit(0);
}

原理:如果所有系数为 0,但最后的结果不是 0,则一定无解。

最后的 AC 代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,x[105][105];
long double a[105][105];
long double ans[105];
long double zero = 1e-6;
signed main(){
    cin >> n;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)cin >> x[i][j];
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n+1;j++)a[i][j] = x[i][j]*1.0;
    }
    for(int i = 1;i<n;i++){
        for(int j = 1;j<=n-i;j++){
            if(fabs(a[j][n-i+1])<=zero)continue;
            long double x = a[j+1][n-i+1]/a[j][n-i+1];
            if(fabs(x)<=zero){
                for(int k = 1;k<=n+1;k++)swap(a[j][k],a[j+1][k]);
                continue;
            }
            for(int k = 1;k<=n+1;k++)a[j][k]*=x;
            for(int k = 1;k<=n+1;k++)a[j][k]-=a[j+1][k];
        }
    }
    for(int i = 1; i <= n; i++){
        bool flag = true;
        for(int j = 1; j <= n; j++) if(a[i][j]) flag = false;
        if(flag && a[i][n+1]) puts("-1"), exit(0);
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(a[i][j]!=0)fp = 0;
        if(fp){
            if(fabs(a[i][n+1])>zero)cout << "-1";
            else cout << "0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        bool fp = 1;
        for(int j = 1;j<=n;j++)if(fabs(a[j][i])>zero)fp = 0;
        if(fp){
            if(a[i][n+1]!=0)cout << "-1";
            else cout<<"0";
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<i;j++){
            a[i][n+1]-=ans[j]*a[i][j];
        }
        ans[i] = a[i][n+1]/a[i][i];
    }
    for(int i = 1;i<=n;i++)if(fabs(ans[i])<=zero)ans[i] = 0;
    for(int i = 1;i<=n;i++)printf("x%lld=%.2Lf\n",i,ans[i]);
    return 0;
}

by Grow2011 @ 2024-07-23 13:13:10

@zhang_kevin thx


|