输出的是inf!!!求救!!!

P5726 【深基4.习9】打分

Wen_Yun @ 2023-04-30 09:44:06


using namespace std;
int main(){
    double max=0,min=10000,n,q,s,j=0;
    cin>>n;
    while(n!=0){
        cin>>q;
        if(max<q)max=q;
        if(min>q)min=q;
        j=j+q;
        n-=1;
    }
    j=j-(max+min);
    s=j/n;
    printf("%.2f",s);
    return 0;
    }

by Milthm @ 2023-04-30 09:51:54

你的n到最后除法的时候,因为前面经过了while循环,现在n肯定是0,然后除以零就返回inf了


by Milthm @ 2023-04-30 09:52:03

@Wangyuqi123


by Wen_Yun @ 2023-04-30 10:30:53

谢谢谢谢谢谢谢谢,老师讲了一遍,膜拜大佬!!!


using namespace std;
int main(){
    double max=0,min=10000,n,q,s,j=0,b;
    cin>>n;
    b=n-2;
    while(n>=1){
        cin>>q;
        if(max<q)max=q;
        if(min>q)min=q;
        j=j+q;
        n-=1;
    }
    j=j-(max+min);
    s=j/b;
    printf("%.2f",s);
    return 0;
    }

by Terry2011 @ 2023-06-01 14:36:52

#include<bits/stdc++.h>
using namespace std;
int main(){
    double max=0,min=10000,n,q,s,j=0,b;
    cin>>n;
    b=n-2;
    while(n>=1){
        cin>>q;
        if(max<q)max=q;
        if(min>q)min=q;
        j=j+q;
        n-=1;
    }
    j=j-(max+min);
    s=j/b;
    printf("%.2f",s);
    return 0;
    }

|