求大佬看看我哪里错了

P5726 【深基4.习9】打分

chenqile @ 2023-12-22 19:21:24

代码如下

#include <bits/stdc++.h>
using namespace std;
int main(){
    double a[1002], n, sum = 0;
    cin>>n;
    for (int i = 0; i < n; i++) cin>>a[i];
    sort(a, a + n);
    for (int i = 1; i < n - 1; i++) sum += a[i];
    printf("%.1lf", sum/n);
    return 0;
}

by xiaoshumiao @ 2023-12-22 19:24:59

@chenqile 仔细读题,他让你保留两位小数。


by 7The_shy @ 2023-12-22 19:27:21

#include <bits/stdc++.h>
using namespace std;
int main(){
    double a[1002],sum = 0;
    int n;
    cin>>n;
    for (int i = 0; i < n; i++) cin>>a[i];
    sort(a, a + n);
    for (int i = 1; i < n - 1; i++) sum += a[i];
    printf("%.2lf", sum/(n-2));
    return 0;
}

by chenqile @ 2023-12-22 19:28:45

怎么还是ce


by 7The_shy @ 2023-12-22 19:28:56

1.n不能为double

2.输出的时候n应该减去两人

3.保留两位应该是“%.2lf”


by xiaoshumiao @ 2023-12-22 19:29:34

@chenqile ?IDE 上没有 CE。


by xiaoshumiao @ 2023-12-22 19:30:23

@7The_shy 不过 n 硬要开 double 也没事。


by 7The_shy @ 2023-12-22 19:31:12

不过本地会报错


by chenqile @ 2023-12-22 19:34:35

THANKS EVERYBODY

我赏1个关注


|