pwq123 @ 2023-12-18 15:07:06
#include<bits/stdc++.h>
using namespace std;
int main(){
double n,maxs=-1,mins=999999,ave,sum,num=-1;
double a[1000];
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
maxs=max(maxs,a[i]);
mins=min(mins,a[i]);
}
for(int i=1;i<=n;i++){
ave+=a[i];
}
ave=(ave-maxs-mins)/(n-2);
for(int i=1;i<=n;i++){
if(maxs!=a[i]&&mins!=a[i])
sum=max(num,fabs(a[i]-ave));
}
cout<<fixed<<setprecision(2)<<ave<<" "<<sum;
return 0;
}
by XyGetItRightAker @ 2023-12-28 14:06:51
sum=max(sum,fabs(a[i]-ave));
看看这个能理解嘛,你代码中是num,应该是sum才对。
by XyGetItRightAker @ 2023-12-28 14:09:25
第二,假设我的数据是
5
11.00 11.00 11.00 11.00 11.00
那么你的最大值和最小值相同,并且在最后循环找sum的之前的判断都是false
by XyGetItRightAker @ 2023-12-28 14:12:10
这是给你调好的ac代码,请不要复制粘贴
#include<bits/stdc++.h>
using namespace std;
int main() {
double n, maxs = -1, mins = 99999999, ave = 0, sum = 0, num = -1;
double a[1000];
int index_max, index_min;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (maxs < a[i]) {
index_max = i;
maxs = a[i];
}
if (mins > a[i]) {
index_min = i;
mins = a[i];
}
ave += a[i];
}
ave = (ave - maxs - mins) / (n - 2);
for (int i = 1; i <= n; i++) {
if (i != index_max && i != index_min) {
sum = max(sum, fabs(a[i] - ave));
}
}
printf("%0.2lf %0.2lf", ave, sum);
return 0;
}
by XyGetItRightAker @ 2023-12-28 14:14:16
说明一下,index_max和index_min是用来记录最大值和最小值下标的。
假设你遇到这样的数据
5
11.00 11.00 13.00 15.00 15.00