为什么sum求和读到输入不是输入的数字而是别的数字?

P5726 【深基4.习9】打分

fengfenglong @ 2022-09-10 11:09:24


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <bits/stdc++.h>
#include <math.h>
#include <string>
#include <cstring>

using namespace std;
//#define PI 3.1415926
double dafen(int n)
{
double sum=0;
int a[1010],max=-1,min=11,i=0;
while(i < n)
{   
    cin >> a[i];
    sum += a[i];
    i++;
    if(a[i] >= max) max = a[i];
    else if(a[i] <= min) min = a[i];
}
sum = sum - max - min;
cout << sum <<endl;
cout << fixed << setprecision(2) << sum/n;
}
int main()
{
int n;
cin >> n;
dafen(n);
return 0;
}

by TankYu @ 2022-09-10 11:16:53

没太听懂您的意思,但是i++应放在while中最后一行后,同时将double dafen(int n)改成void dafen(int n)

另外cout << fixed << setprecision(2) << sum/n;改成cout << fixed << setprecision(2) << sum/(n - 2);


by fengfenglong @ 2022-09-12 09:38:49

@TankYu 就是我i++从sum求和那一行放到了while中的最后一行,为什么求和结果就变了?我放在上面用示例来做得到sum=5而放到while后面就是23这是为什么?


|