WA???

P5726 【深基4.习9】打分

hp2024022 @ 2024-12-09 21:09:35

#include<bits/stdc++.h>
using namespace std;
int main(){
    double n,a,maxn = -11,minn = 99,sum = 0;
    cin>>n;
    for (int i = 1;i<=n;i++){
        cin>>a;
        if (a>maxn){
            maxn = a;
        }
        if (a<minn){
            minn = a;
        }
        else{
            sum+=a;
        }
    }
    printf("%.2lf",sum/(n-2));
}

哪错了???


by KarmaticEnding @ 2024-12-09 21:14:09

@hp2024022

小伙子测过这组样例吗?

3
1 2 3

by ZMY_123 @ 2024-12-09 21:30:32

#include<bits/stdc++.h>
using namespace std;

int main(){
    double n,a[1005],maxn = -11,minn = 99,sum = 0,maxi,mini;
    cin>>n;
    for (int i = 1;i<=n;i++){
        cin>>a[i];
        if (a[i]>maxn){
            maxn = a[i];
            maxi = i;
        }
        if (a[i]<minn){
            minn = a[i];
            mini = i;
        }

    }
    for (int i = 1;i<=n;i++){
        if (i!=maxi && i!=mini){
            sum+=a[i];
        }
    }
    printf("%.2lf",1.00*sum/(n-2.00));
}

你的代码的改进版,可AC(你看了应该就知道你错哪了)

求关


by hp2024022 @ 2024-12-10 15:29:00

@KarmaticEnding 没有......


by ZMY_123 @ 2024-12-11 19:12:47

@hp2024022

你还没有找出输入的最大值和最小值就将sum+=a;

也就是说输入的这个数只要没有之前输入最大的数大、没有之前输入最小的数小就将 sum+=a;所以不对


by hp2024022 @ 2024-12-11 21:08:05

@ZMY_123 谢谢


|