求问sort?

P5726 【深基4.习9】打分

CY666 @ 2020-10-16 00:02:24

#include<bits/stdc++.h>
using namespace std;
int n;
int 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;
 } 

为什么sort括号里面不加1会由两个wa


by 已注销yj!gBUM @ 2020-10-16 00:03:53

数组下标的问题

默认的话是从0开始


by TKater_yzt @ 2020-10-16 00:45:06

不加1你排的是0~n-1中的数


by cxqghzj @ 2020-10-16 07:16:27

2楼正解

实际上不需要排序的

只需要算出最大和最小

把全部的加起来再减去最大和最小就行了


by cxqghzj @ 2020-10-16 07:17:05

我的代码/xyx

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int n,s[1001] = {0},maxn = -1,minn = 11;
    double h = 0;
    cin >> n;
    for (int i = 0;i < n;i++){
        cin >> s[i];
        maxn = max(s[i],maxn);
        minn = min(s[i],minn);
        h += s[i];
    }
    h -= maxn;
    h -= minn;
    h = h / (n - 2);
    printf("%.2lf",h);
    return 0;
}

by liuzimingc @ 2020-11-02 20:00:00

@CY666 数组范围不是0~n-1吗……

#include<cstdio>
int main(){
    int n,max=-100,min=100,tot=0;
    scanf("%d",&n);
    int a[n];
    for (int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        tot+=a[i];
        if (a[i]>max)
            max=a[i];
        if (min>a[i])
            min=a[i];
    }
    printf("%.2lf",(tot-max-min)*1.0/(n-2));
    return 0;
}

|