ONicklgzh @ 2023-12-11 12:42:33
#include <stdio.h>
#include <math.h>
double arr[500];
int main(){
int N,i;
double sum1=0,sum2=0,max=-1,min=999999999,aver2=0,max2=0;
scanf("%d",&N);
for(i=1;i<=N;i++){
scanf("%lf",&arr[i]);
if(max<arr[i]) max=arr[i];
if(min>arr[i]) min=arr[i];
sum1+=arr[i];
}
sum2=sum1-max-min;
aver2=sum2/(N-2);
for(i=1;i<=N;i++){
if(arr[i]==max||arr[i]==min){
continue;
}
if(fabs(arr[i]-aver2)>max2){
max2=fabs(arr[i]-aver2);
}
}
printf("%.2lf %.2lf",aver2,max2);
return 0;
}
by char_cha_ch_ @ 2023-12-11 13:15:43
先放代码(已改)
#include <stdio.h>
#include <math.h>
double arr[500];
int main(){
int N,i;
double sum1=0,sum2=0,maxu=-1,minu=-1,maxx=-1,minn=99999999999,aver2=0,max2=-1;
scanf("%d",&N);
for(i=1;i<=N;i++){
scanf("%lf",&arr[i]);
if(maxx<arr[i]) maxx=arr[i],maxu=i;
if(minn>arr[i]) minn=arr[i],minu=i;
sum1+=arr[i];
}
sum2=sum1-maxx-minn;
aver2=sum2/(N-2);
for(i=1;i<=N;i++){
if(i==maxu||i==minu){
continue;
}
if(fabs(arr[i]-aver2)>max2){
max2=fabs(arr[i]-aver2);
}
}
printf("%.2lf %.2lf",aver2,max2);
return 0;
}
by char_cha_ch_ @ 2023-12-11 13:17:16
我觉得问题主要就是要是有多个最大值和最小值你用最大值和最小值去判断的话就都会去掉(17行),应该是存下标
by char_cha_ch_ @ 2023-12-11 13:18:29
@ONicklgzh 其实可以直接sort的……
by ONicklgzh @ 2023-12-11 19:19:27
@_zzdmz 十分十分十分感谢