大佬们帮我看看这个问题出现在哪

P5726 【深基4.习9】打分

sy1234567887 @ 2023-04-17 12:04:50

#include <iostream>
#include<stdio.h>
int main() {
    int n,x,y,a[100000],i;
    double s=0,t;
    scanf("%d",&n);
    for (i=1; i<=n; i++) 
    scanf("%d",&a[i]);
     for (i=1; i<=n; i++) 
     { if(a[1]>a[i])
        {
            x=a[1];
            a[1]=a[i];
            a[i]=x;
        }
      }
      for (i=2; i<=n; i++) 
     { if(a[2]<a[i])
        {
            y=a[2];
            a[2]=a[i];
            a[2]=y;
        }
      }
      for (i=3; i<=n; i++) 
      s=s+a[i];
       t=s/(n-2);
       printf("%.2lf",t);
       return 0;
}

by Henry2012 @ 2023-04-17 13:12:11

        y=a[2];
            a[2]=a[i];
            a[2]=y;

应该是

        y=a[2];
            a[2]=a[i];
            a[i]=y;

by Score_Elevate @ 2023-09-01 20:33:51

@sy1234567887 已经改好了,求关注

#include <bits/stdc++.h>
#define MAXN 1000
using namespace std;
int n;
int s;
int a[MAXN + 5];
inline int read()
{
    int x = 0, y = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
      if (c == '-')
        y = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      x = x * 10 + c - '0', c = getchar();
    return x * y;   
}
int main() {
    n = read();
    for (int i = 1; i <= n; i++) 
      a[i] = read();
      //大概明白你的意思了,以a[1]为max,a[2]为min,但是过于复杂了,在比赛中容易TLE,把a[3]之后的加起来
    for (int i = 2; i <= n; i++) 
      if (a[i] > a[1]) swap (a[i], a[1]);//swap函数可以直接将两个数交换数值看,之前的判断是反的
    for (int i = 2; i<=n; i++) 
      if (a[i] < a[2]) swap (a[i], a[2]);
    for (int i = 3; i <= n; i++) 
      s += a[i];
    printf("%.2lf",double (s * 1.0 / (n - 2)));
       return 0;
}

|