玄学的东西

B2054 求平均年龄

gaomingshuo @ 2025-01-02 20:38:53

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a;
    double ans=0.0;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>a;
        ans+=a;
    }
    printf("%0.2lf",ans/n);
    return 0;
}

为啥ans改成float是95分??

请大佬指点,谢谢!


by computer11 @ 2025-01-02 20:49:45

因为精度不够,并且最后的%lfdouble用的。


by zzhengxi @ 2025-01-02 21:09:05

@gaomingshuo 代码中尽量使用double,不要用float


by gaomingshuo @ 2025-01-02 21:25:26

@computer11@zzhengxi 谢谢!


|