请帮忙看一下为什么会错,谢谢。

P5726 【深基4.习9】打分

DevilsFlame @ 2023-08-08 12:27:52

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

by DevilsFlame @ 2023-08-08 12:45:43

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a[101],maxn=0,minn=0;
    double h=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        h+=a[i];
    for(int i=1;i<=n;i++){
        maxn=max(maxn,a[i]);
        minn=min(minn,a[i]);
        }
    }
    cout<<fixed<<setprecision(2)<<(h-maxn-minn)/(n-2)<<endl;
}

by DevilsFlame @ 2023-08-08 12:46:52

@ybc2025125chenyuyang 接下来的错误是什么?


by DevilsFlame @ 2023-08-08 12:47:39

3
1 2 3
-1962309254.00

--------------------------------
Process exited with return value 0
Press any key to continue . . .

by ybc2025chenyuyang @ 2023-08-08 12:52:38

@yhdxg

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a[1001],maxn=0,minn=INT_MAX;
    double h=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        h+=a[i];
        maxn=max(maxn,a[i]);
        minn=min(minn,a[i]);
    }
    cout<<fixed<<setprecision(2)<<(h-maxn-minn)*1.0/(n-2)<<endl;
}

1.数组开小了

2.找最大最小值无需再加一层循环,直接在输入时就行了

3.minn应该定义成最大值

4.输出时应转化成double,乘一个1.0


by dienter @ 2023-08-08 12:53:32

@yhdxg 全改成double,你个整数类型除以个double类型?


上一页 |