AKkkk_TIX @ 2023-10-04 19:26:43
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,ii,maxnum = 2,minnum = 11;
double all;
cin >> n;
for (int i = 0; i < n; i++){
cin >> ii;
if (ii > maxnum){
maxnum = ii;
continue;
}
if (ii < minnum){
minnum = ii;
continue;
}
all += ii;
}
n -= 2;
all /= n;
cout << fixed << setprecision(2) << all;
return 0;
}
我觉得我的代码没问题啊~
但是
我只是蒟蒻,求助各位大佬
by PF_anyone @ 2023-10-05 22:02:20
maxnum的初始化应该是最小的,题目数据从0到10,可能会有全部分数小于2的情况发生
by IAKIOIpwp @ 2023-10-18 20:23:59
两个if语句如果直接跳过的话很可能跳过多个而不是两个
by IAKIOIpwp @ 2023-10-18 20:26:26
n减了2而all很可能不是n-2个数的和
by IAKIOIpwp @ 2023-10-18 20:31:29
建议用个sum来记录和,用和减去最大值和最小值再除以n-2
by IAKIOIpwp @ 2023-10-18 20:38:19
代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,ii,maxnum = 0,minnum = 11,sum = 0;
double all;
cin >> n;
for (int i = 0; i < n; i++){
cin >> ii;
if (ii > maxnum){
maxnum = ii;
}
if (ii < minnum){
minnum = ii;
}
sum += ii;
}
sum = sum - minnum - maxnum;
n -= 2;
all = (double) sum / n;
cout << fixed << setprecision(2) << all;
return 0;
}
AC了