玄学求助

P2455 [SDOI2006] 线性方程组

chenziqian @ 2020-12-31 17:13:27

rt,程序1挂了,程序2把程序1的break删了就过了

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<climits>
#include<vector>
//#include<bits/stdc++.h>
#define eps (1e-7)
using namespace std;
int n;
double mat[503][503];
void print(){
    printf("*******\n");
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            if(mat[i][j]>eps) cout<<mat[i][j]<<" ";
            else cout<<"0 ";
        }
        cout<<endl;
    }
    printf("*******\n");
}
int gauss(){
    for(int j=1;j<=n;j++){
        for(int i=j;i<=n;i++){
            if(fabs(mat[i][j])>eps){
                for(int k=1;k<=n+1;k++){
                    swap(mat[i][k],mat[j][k]);
                }
                break;
            }
        }
        //print();
        if(fabs(mat[j][j])<=eps) continue;
        for(int i=1;i<=n;i++){
            if(i==j) continue;
            double rate=mat[i][j]/mat[j][j];
            for(int k=1;k<=n+1;k++){
                mat[i][k]-=rate*mat[j][k];
            }
        }
    }
    //print();
    for(int i=1;i<=n;i++){
        bool f=1;
        for(int j=1;j<=n;j++){
            if(fabs(mat[i][j])>eps){
                f=0;
                break;
            }
        }
        if(f&&fabs(mat[i][n+1])>eps) return -1;
    }
    for(int i=1;i<=n;i++){
        if(fabs(mat[i][i])<=eps&&fabs(mat[i][n+1])<=eps) return 0;
    }
    return 1;
}
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    //ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            cin>>mat[i][j];
        }
    }
    int res=gauss();
    if(res!=1){
        cout<<res;
        return 0;
    }
    //print();
    /*bool no=0,inf=0;
    for(int i=1;i<=n;i++){
        int j=1;
        while(fabs(mat[i][j])<=eps&&j<=n+1) j++;
        if(j>n+1) inf=1;
        if(j==n+1) no=1;
    }
    if(no){
        printf("-1");
        return 0;
    }
    if(inf){
        printf("0");
        return 0;
    }*/
    for(int i=1;i<=n;i++){
        if(fabs(mat[i][n+1]/mat[i][i])<=eps){
            printf("x%d=0.00\n",i);
        }
        printf("x%d=%.2f\n",i,mat[i][n+1]/mat[i][i]);
    }
    return 0;
}
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<climits>
#include<vector>
//#include<bits/stdc++.h>
#define eps (1e-7)
using namespace std;
int n;
double mat[503][503];
void print(){
    printf("*******\n");
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            if(mat[i][j]>eps) cout<<mat[i][j]<<" ";
            else cout<<"0 ";
        }
        cout<<endl;
    }
    printf("*******\n");
}
int gauss(){
    for(int j=1;j<=n;j++){
        for(int i=j;i<=n;i++){
            if(fabs(mat[i][j])>eps){
                for(int k=1;k<=n+1;k++){
                    swap(mat[i][k],mat[j][k]);
                }
            }
        }
        //print();
        if(fabs(mat[j][j])<=eps) continue;
        for(int i=1;i<=n;i++){
            if(i==j) continue;
            double rate=mat[i][j]/mat[j][j];
            for(int k=1;k<=n+1;k++){
                mat[i][k]-=rate*mat[j][k];
            }
        }
    }
    //print();
    for(int i=1;i<=n;i++){
        bool f=1;
        for(int j=1;j<=n;j++){
            if(fabs(mat[i][j])>eps){
                f=0;
                break;
            }
        }
        if(f&&fabs(mat[i][n+1])>eps) return -1;
    }
    for(int i=1;i<=n;i++){
        if(fabs(mat[i][i])<=eps&&fabs(mat[i][n+1])<=eps) return 0;
    }
    return 1;
}
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    //ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            cin>>mat[i][j];
        }
    }
    int res=gauss();
    if(res!=1){
        cout<<res;
        return 0;
    }
    //print();
    /*bool no=0,inf=0;
    for(int i=1;i<=n;i++){
        int j=1;
        while(fabs(mat[i][j])<=eps&&j<=n+1) j++;
        if(j>n+1) inf=1;
        if(j==n+1) no=1;
    }
    if(no){
        printf("-1");
        return 0;
    }
    if(inf){
        printf("0");
        return 0;
    }*/
    for(int i=1;i<=n;i++){
        if(fabs(mat[i][n+1]/mat[i][i])<=eps){
            printf("x%d=0.00\n",i);
        }
        printf("x%d=%.2f\n",i,mat[i][n+1]/mat[i][i]);
    }
    return 0;
}

by chenziqian @ 2020-12-31 17:14:19

是一向毒瘤的数据被顺序刚好卡过去了?


by Guoyh @ 2020-12-31 17:22:20

玄学


by chenziqian @ 2021-01-02 12:43:03

数论题都没人看吗


by chenziqian @ 2021-02-01 11:00:20

都一年多了,还没有人吗


|