0分求助

B2095 白细胞计数

biometric @ 2022-09-03 20:36:21

code:

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

int main()
{
    int a;
    double avg, maxn = 0, minn = 1e7, ans;
    cin >> a;
    for (int i = 1; i <= a; i++)
    {
        double x;
        cin >> x;
        avg += x;
        maxn = max(maxn, x);
        minn = min(minn, x);
    }
    avg /= a;
    ans = max(maxn - avg, avg - minn);
    cout << fixed << setprecision(2) << avg << endl << fixed << setprecision(2) << ans;
    return 0;
}

by xs_siqi @ 2022-09-03 20:39:09

@biometric 题意不计最大值最小值。但您貌似记了。


by xs_siqi @ 2022-09-03 20:39:55

并且,输出的不是空格吗、、


by xs_siqi @ 2022-09-03 21:09:12

@biometric

推荐用数组。但不用也没关系,维护次大值次小值也能解决问题。

代码帮您改了。

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

int main()
{
    int a;
    double avg = 0, maxn = -1e9-5, minn = 1e9+5, ans = 0,max2=-1e9-5,min2=1e9+5;
    cin >> a;
    for (int i = 1; i <= a; i++)
    {
        double x;
        cin >> x;
        avg += x;
        if(x>=maxn)max2=max(maxn,max2),maxn=x;
        if(x>=max2&&x<maxn)max2=x;
        if(x<=minn)min2=min(minn,min2),minn=x;
        if(x<=min2&&x>minn)min2=x;
    }
    avg -= (maxn + minn);
    avg /= (a-2);
    avg=fabs(avg);
    max2=((max2==-1e9-5)?avg:max2);
    min2=((min2==1e9+5)?avg:min2);
    ans=max(fabs(max2-avg),fabs(avg-min2));
    cout << fixed << setprecision(2) << avg << " " << fixed << setprecision(2) << ans;
    return 0;
}

by biometric @ 2022-09-05 18:22:12

谢谢!@xs_siqi


|