C++全WA求助,不知道哪里错了

P5726 【深基4.习9】打分

doggy38 @ 2024-08-07 21:39:06

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    int p[n];
    for(int i=0;i<=n-1;i++)scanf("%d",&p[i]);
    sort(p,p+n);
    for(int i=1;i<=n-2;i++)sum=sum+p[i];
    double point=sum/(n-2);
    printf("%.2f",point);
    return 0;
 } 

by liruizhou_lihui @ 2024-08-07 21:42:51

冷恣肆:普通数组sort之后最后两个是第一大和第二大


by liruizhou_lihui @ 2024-08-07 21:44:32

(刚学写的勿喷)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,max=-1,min=11,s=0;
    double pj=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a>max) max=a;
        if(a<min) min=a;
        s+=a;
    }
    pj=(s-max-min)*1.0/(n-2);
    cout<<fixed<<setprecision(2)<<pj;
    return 0;
}

by haimingbei @ 2024-08-07 21:47:35

@doggy38

(AC,求关注)

按你代码改的,第十二行

double point=sum/(n-2) 应该为

double point=sum*1.0/(n-2)

要乘1.0!!!!!!!!!

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    int p[n];
    for(int i=0;i<=n-1;i++)scanf("%d",&p[i]);
    sort(p,p+n);
    for(int i=1;i<=n-2;i++)sum=sum+p[i];
    double point=sum*1.0/(n-2);
    printf("%.2f",point);
    return 0;
 }

by haimingbei @ 2024-08-07 21:48:02

@liruizhou123 ???


by wangxhiyan114514 @ 2024-08-07 21:56:48

让我贴个代码

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

by doggy38 @ 2024-08-08 11:11:03

@liruizhou123 涨知识了?


by doggy38 @ 2024-08-08 11:13:54

@haimingbei 谢谢你的解答,已AC,请问能不能告诉我原理?


by doggy38 @ 2024-08-08 11:15:55

@haimingbei 哦我好像懂了,谢谢?


by haimingbei @ 2024-08-08 12:14:08

@doggy38 因为double类型除以int类型会自动化为int,应为int类型级别最高,所以要乘1.0


by PengPeng2 @ 2024-08-20 12:11:07

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i = 1; i <= n;i++){
        cin>>a[i];
    }
    sort(a + 1,a + n + 1);
    long double ans = 0;
    for(int i = 2; i <= n - 1;i++){
        ans += a[i];
    }
    cout << fixed << setprecision(2) << ans / (n - 2);
    return 0;
}

|