大佬们可以帮忙看看为什么只有六十分吗

P5726 【深基4.习9】打分

LacusMortis @ 2024-10-24 22:39:16

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
    int n,a[1000],sum=0;
    cin>>n;
    for (int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n-1);
    for (int i=1;i<n-1;i++)
        sum=sum+a[i];
    double x=sum/(n-2.0);
    cout<<fixed<<setprecision(2);
    cout<<x;
    return 0;
}

by wyz0326 @ 2024-10-24 22:43:46

你的sum和X全是整形int,而你最后的是double类型数据,所以在转换时会有误差出现


by wyz0326 @ 2024-10-24 22:46:31

@wyz0326 在输出时候一起搞就行

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

by wyz0326 @ 2024-10-24 22:47:25

@LacusMortis 你那好像有个int-2.0


by lxr_Galaxy @ 2024-10-24 22:48:47

@LacusMortis 改成这样就好了,double会掉精度

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main(){
    int n;
    float a[1000],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=sum+a[i];
//      cout<<a[i]<<" ";
    }

//  cout<<sum<<" ";
    float x=sum/(n-2.0);
    cout<<fixed<<setprecision(2)<<x; 
    return 0;
}

by wyz0326 @ 2024-10-24 22:58:28

@LacusMortis 刚才给你debug了一下 你排序少一个所以出问题 得改成

sort(a,a+n);

你自己用这两个程序输出下就明白了

你的

include<iostream>

include<algorithm>

include<iomanip>

using namespace std; int main() { int n,a[1000],sum=0; cin>>n; for (int i=0;i<n;i++) cin>>a[i]; sort(a,a+n-1); for (int i=1;i<n-1;i++){ sum=sum+a[i]; cout<<sum<<endl; }

double x=sum/(n-2.0);
cout<<fixed<<setprecision(2);
cout<<x;
return 0;

}

#### 改了后的

include<iostream>

include<algorithm>

include<iomanip>

using namespace std; int main() { int n,a[1000],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=sum+a[i]; cout<<sum<<endl; }

double x=sum/(n-2.0);
cout<<fixed<<setprecision(2);
cout<<x;
return 0;

}


希望有帮助

by LacusMortis @ 2024-10-25 23:05:55

@wyz0326 懂了,非常感谢


|