95分求助

B2054 求平均年龄

Jiang2012 @ 2024-08-04 11:14:57

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

by HeartBeast @ 2024-08-04 11:24:21

先算和再除n,ans开double

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

|