请问各位大佬该怎么改,sum和n-2都没问题,但是结果不对

P5726 【深基4.习9】打分

zlh202483023 @ 2024-11-16 22:41:59

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
    int n,i,j,p,sum=0, arr[10005] = { 0 };
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> arr[i];

    }
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                p = arr[j+1];
                arr[j + 1] = arr[j];
                arr[j] = p;
            }
        }
    }
    for (i = 1; i < n-1; i++) {
        sum = sum + arr[i];
    }
    cout << fixed << setprecision(2) << double(sum/(n-2)) ;
    return 0;
}

by Luo_Huanqi @ 2024-11-16 22:53:03

输出的时候sum先 * 1.0, 因为 sum/(n - 2) 仍然是整数, 转换成 double 小数部分是 .00


by Luo_Huanqi @ 2024-11-16 22:54:18

即改为:

cout << fixed << setprecision(2) << double(1.0 * sum / (n - 2));

by taojinqi123456 @ 2024-12-16 21:01:33

这个简单,sum/(n-2)还是整数,所以double浮点数部分是.00 我的代码是这样的:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
    int x,x1[10000],d=0,xiao=0;
    float cnt=0.00;
    cin>>x;
    for(int i=0;i<x;i++){
        cin>>x1[i];
        if(x1[i]>x1[d]) d=i;
        if(x1[i]<x1[xiao])  xiao=i;
        cnt+=x1[i];
    }
    cnt-=x1[d];
    cnt-=x1[xiao];
    cnt/=x-2;
    printf("%.2f",cnt);
    return 0;
}

懂了吗


|