wmyQAQ @ 2023-08-02 12:25:33
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n;
double a[305],sum=0,max=0,min,ping,cha=0.00;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(i==1) min=a[i];
sum+=a[i];
if(a[i]>max) max=a[i];
if(a[i]<min) min=a[i];
}
ping=(sum-max-min)/(n-2);
for(int i=1;i<=n;i++)
{
if((a[i]<max)&&(a[i]>min)&&(fabs(a[i]-ping)>cha))
cha=fabs(a[i]-ping);
}
printf("%.2lf %.2lf",ping,cha);
return 0;
}
by wininaction @ 2023-08-02 12:59:28
去除 一个 数值最大的样本和 一个 数值最小的样本,所以要保留一个最大/小的下标 代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double a[305],sum=0,maxx=-1,minn=9999999,ping,cha=-1,maxi,mini;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(a[i]>maxx) maxx=a[i],maxi=i;
if(a[i]<minn) minn=a[i],mini=i;
}
ping=(sum-maxx-minn)/(n-2);
for(int i=1;i<=n;i++)
{
if((i!=maxi)&&(i!=mini)&&(fabs(a[i]-ping)>cha))
cha=fabs(a[i]-ping);
}
printf("%.2lf %.2lf",ping,cha);
return 0;
}
by wmyQAQ @ 2023-08-02 15:22:51
@wininaction 感谢dalao