求教为啥只有部分对

B2095 白细胞计数

v_ever @ 2024-11-15 22:59:53

#include<stdio.h>
#include<math.h>
double fun(double a[301],int n);
double fun2(double a[301],int n);
int main()
{
    int n;
    double a[301];
    double b[301];
    int i,j;
    double sum=0,ave,max,min,max2,min2;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf",&a[i]);
    }
    max=fun(a,n);
    min=fun2(a,n);
    for(i=0,j=0;i<n;i++)
    {
        if(a[i]!=max&&a[i]!=min)
        {
            b[j]=a[i];
            j++;
        }
    }
    for(i=0;i<j;i++)
    {
        sum+=b[i];
    }
    ave=sum/j;
    max2=fabs(fun(b,j)-ave);
    min2=fabs(fun2(b,j)-ave);
    if(max2>=min2)
        printf("%.2lf %.2lf",ave,max2);
    else
        printf("%.2lf %.2lf",ave,min2);
    return 0;
}
double fun(double a[301],int n)
{
    double max=a[0];
    int i;
    for(i=1;i<n;i++)
    {
        if(max<a[i])
            max=a[i];
    }   
    return max;
}
double fun2(double a[301],int n)
{
    double min=a[0];
    int i;
    for(i=1;i<n;i++)
    {
        if(min>a[i])
            min=a[i];
    }   
    return min;
}

by pff20121005 @ 2024-11-23 15:19:40

#include<iostream>
#include<cmath>
using namespace std;
double a[100000];
int main(){
    int n,maxi = 1,mini = 1;
    double y = 0,maxn = -1,sum = 0;
    cin>>n;
    for(int i = 1;i <= n;i++){
        cin>>a[i];
        sum += a[i];
        if(a[i] > a[maxi])maxi = i;
        if(a[i] < a[mini])mini = i; 
    }
    y = (sum - a[maxi] - a[mini]) / (n - 2.0);
    for(int i = 1;i <= n;i++){
        if(i != maxi && i != mini){
            if(fabs(a[i] - y) > maxn)maxn = fabs(a[i] - y);
        }
    }
    printf("%.2lf %.2lf",y,maxn);
    return 0;
}

|