yjy_fywy @ 2024-12-11 21:24:54
#include <bits/stdc++.h>
#include <string>
#include <algorithm>
using namespace std;
int main(){
long long a,sum=0;
cin>>a;
double aa[a],b[2]={-1.0},c[2]={248248.0},ans=0.0;
for(int i=0;i<a;i++){
cin>>aa[i];
}
for(int i=0;i<a;i++){
if(b[0]<aa[i]){
b[0]=aa[i],b[1]=i*1.0;
}
if(c[0]>aa[i]){
c[0]=aa[i],c[1]=i*1.0;
}
}
aa[int(b[1])]=0.0,aa[int(c[1])]=0.0;
for(int i=0;i<a;i++){
ans+=aa[i];
}
ans/=(a-2);
for(int i=0;i<a;i++){
if(b[0]<aa[i]){
b[0]=aa[i];
}
}
cout<<fixed<<setprecision(2)<<ans<<" "<<b[0]-ans;
return 0;
}
by sinotang @ 2024-12-18 22:06:42
用math库里的fabs函数就行了
#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("%.2f %.2f",y,maxn);
return 0;
}