大家一起来找不同

灌水区

liruizhou_lihui @ 2025-01-10 23:43:11

只需要找gauss函数的不同,std1是这题的WA代码,std2是这题的AC代码

std1

#include<bits/stdc++.h>
using namespace std;
const int N=160;
const double eps=1e-6;
double a[N][N];
int n;
int gauss()
{
    int c,r;
    for(c=0,r=0;c<n;c++)
    {
        int t=r;
        for(int i=r;i<n;i++)
        {
            if(fabs(a[i][c])>fabs(a[t][c]))
            {
                t=i;
            }
        }
        if(fabs(a[t][c])<eps)
        {
            continue;
        }
        for(int i=c;i<=n;i++)
        {
            swap(a[r][i],a[t][i]);
        }
        for(int i=n;i>=c;i--)
        {
            a[r][i]/=a[r][c];
        }
        for(int i=0;i<n;i++)
        {
            if(fabs(a[i][c])>eps && i!=r)
            {
                for(int j=n;j>=c;j--)
                {
                    a[i][j]-=a[i][c]*a[r][j];
                }
            }
        }
        r++;
    }
    if(r<n)
    {
        for(int i=r;i<n;i++)
        {
            if(fabs(a[i][n])>eps)
            {
                return -1;
            }
        }
        return 0;
    }
    return 1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    int ans=gauss();
    if(ans==1)
    {
        for(int i=0;i<n;i++)
        {
            printf("x%d=%.2f\n",i+1,a[i][n]);
        }
    }
    else
    {
        cout<<ans;
    }
    return 0;
}

std2

#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

const int N = 160;
double a[N][N];
const double eps = 1e-6;
int n;

int solve(){
    int c, r;
    for(c = 0, r = 0; c < n; c++){
        int t = r;
        for(int i = r; i < n; i++){
            if(fabs(a[i][c]) > fabs(a[t][c])){
                t = i;
            }
        }

        if(fabs(a[t][c]) < eps) continue;

        for(int i = c; i < n+1; i++){
            swap(a[r][i], a[t][i]);
        }

        for(int i = n; i >= c; i--){
            a[r][i] /= a[r][c];
        }

        for(int i = 0; i < n; i++){
            if(fabs(a[i][c]) > eps && i!=r){
                for(int j = n; j >= c; j--){
                    a[i][j] -= a[i][c] * a[r][j];
                }
            }
        }
        r++;
    }
    if(r < n){
        for(int i = r; i < n; i++){
            if(fabs(a[i][n]) > eps) return -1;
        }
        return 0;
    }

//  for(int i = n-2; i>=0; i--){
//      for(int j = i+1; j < n; j++){
//          a[i][n] -= a[i][j] * a[j][n];
//      }
//  }
    return 1;
}

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j <= n; j++){
            cin >> a[i][j];
        }
    }

    int result = solve();

    if(result == 1){
        for(int i = 0; i < n; i++){
            printf("%.2f\n", a[i][n]);
        }
    }
    else if(result == 0){
        //cout << 0 << endl;
        cout << "No Solution" << endl;
    }
    else if(result == -1){
        //cout << -1 << endl;
        cout << "No Solution" << endl;
    }
}

by _cbw @ 2025-01-11 08:01:30

为啥 i <= ni < n + 1 评测结果会有区别啊(应当只有这处了?


by arrow_king @ 2025-01-11 08:44:42

有没有可能我们 SDOI2006 的数据实在太厉害了。


by sunpengyu @ 2025-01-11 08:50:11

std1这样改就对了@liruizhou_lihui

#include <bits/stdc++.h>
using namespace std;
const int N = 160;
double a[N][N];
const double eps = 1e-6;
int n;
int solve() {
    int c, r;
    for(c = 0, r = 0; c < n; c++) {
        int t = r;
        for(int i = r; i < n; i++) {
            if(fabs(a[i][c]) > fabs(a[t][c])) {
                t = i;
            }
        }
        if(fabs(a[t][c]) < eps) continue;
        for(int i = c; i < n+1; i++) {
            swap(a[r][i], a[t][i]);
        }
        for(int i = n; i >= c; i--) {
            a[r][i] /= a[r][c];
        }
        for(int i = 0; i < n; i++) {
            if(fabs(a[i][c]) > eps && i!=r) {
                for(int j = n; j >= c; j--) {
                    a[i][j] -= a[i][c] * a[r][j];
                }
            }
        }
        r++;
    }
    if(r < n) {
        for(int i = r; i < n; i++) {
            if(fabs(a[i][n]) > eps) return -1;
        }
        return 0;
    }
    return 1;
}
int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= n; j++) {
            cin >> a[i][j];
        }
    }
    int result = solve();
    if(result == 1) for(int i = 0; i < n; i++) cout<<"x"<<i+1<<"="<<a[i][n]<<endl;
    else if(result == 0) cout <<0<< endl;
    else if(result == -1) cout <<-1<< endl;
    return 0; 
}

|