Luochenhao88 @ 2025-01-07 12:15:18
#include<bits/stdc++.h>
using namespace std;
int n,k;
double x,y;
int main(){
cin>>n;
double a[n+1],b[n];
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
if(i!=1&&i!=n){
k++;
b[k]=a[i];
}
y+=a[i];
}
y/=n*1.0;
for(int i=1;i<=k;i++){
x+=b[i];
}
x/=k*1.0;
y-=x;
if(y<0){
y=-y;
}
printf("%.2f %.2f",x,y);
return 0;
}
/*
样例输出:
11.00 0.00
*/
by hepeixi @ 2025-01-07 13:26:04
求关注!
#include<bits/stdc++.h>
using namespace std;
int main(){
double maxx=-999999999,minn=999999999;
double d,c[301],b[301],h=0;
int n,i,v,k;
cin>>n;
for(i=1;i<=n;i++){
cin>>c[i];
h=h+c[i];
if(c[i]>maxx){
maxx=c[i];
k=i;
}
if(c[i]<minn){
minn=c[i];
v=i;
}
}
d=(h-c[v]-c[k])/(n-2.00);
printf("%.2lf",d);
cout<<" ";
for(i=1;i<=n;i++){
if(i!=k&&i!=v){
b[i]=abs(d-c[i]);
}else{
b[i]=-10001;
}
}
maxx=-10001;
for(i=1;i<=n;i++){
if(b[i]>maxx){
maxx=b[i];
}
}
printf("%.2lf",maxx);
return 0;
}
by 0132linzixuan @ 2025-01-07 13:26:16
是b[i]数组每一个数与x相差的最大值才为y不是说不删最大最小值的平均值与原平均值的差 题目理解错了 还要给出该平均值的误差,即所有有效样本(即不包括已扣除的两个样本)与该平均值之差的绝对值的最大值。
by 0132linzixuan @ 2025-01-07 13:27:14
可供参考:
#include<bits/stdc++.h>
using namespace std;
double a[1000000];
int main(){
int n;
cin>>n;
double maxx=0,minn=0x7ffffff;
double am=0;
int w1,w2;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxx) {
maxx=a[i];
w1=i;
}
if(a[i]<minn) {
w2=i;
minn=a[i];
}
am+=a[i];
}
double x=(am-minn-maxx)/(n-2);
double y=0;
for(int i=1;i<=n;i++){
if(w1==i||w2==i) continue;
y=max(fabs(a[i]-x),y);
}
cout<<fixed<<setprecision(2)<<x<<' '<<y;
}
by NIzhenxin2014 @ 2025-01-07 13:28:47
一维数组?