求助!

P5726 【深基4.习9】打分

2345A @ 2022-06-19 11:07:05

自以为没什么问题,但样例都没过

#include<iomanip>
#include <iostream>
using  namespace std;
int main()
{
    int a, b, c = 0,n=0,k=0;//c是最大的数,n是最小数
    double ping = 0;
    cin >> a;
    cin >> b;
    n = b;
    k = k + b;
    for (int i = 2; i <= a; i++)
    {

        cin >> b;
        k = k + b;

        if (b > c)
        {
            c = b;
        }
        if (n > b) 
        {
            n = b;
        }

    }
    ping = (k - c - n)/(a-2) ;
    cout << fixed << setprecision(2) << ping;
    return 0;
}

by zzyxl_qaq @ 2022-06-19 11:19:02

k-c-n(a-2)都是int类型,虽然pingdouble,但int/int是保留整数部分,所以可以改成

ping = (k-c-n)/1.0/(a-2);

by kkk_fans @ 2022-06-19 11:20:47

double a, b, c = 0,n=0,k=0;

by kkk_fans @ 2022-06-19 11:21:12

改成这样也行,这道题的精度是肯定够的


by 2345A @ 2022-06-19 11:21:49

@Slytherin_always 谢谢


by 2345A @ 2022-06-19 11:22:18

@kkk_fans 谢谢


by nikodo @ 2022-07-08 21:41:25

#include<iostream>
#include<iomanip>
using namespace std;
int n,a[1000],big=0,small=11;
int f=0;
float s;
void dafen(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]>big)
            big=a[i];
        if(a[i]<small)
            small=a[i];
        f+=a[i];
    }
    f=f-big-small;
    s=(float)f/(n-2);
    cout<<fixed<<setprecision(2)<<s;
}
int main(){
    dafen();
    return 0;
}

by nikodo @ 2022-07-08 21:42:47

@baiqian


|