95求助

B2054 求平均年龄

lxplxplxplxp @ 2022-09-11 19:17:36

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

by isbfpoiaewfohqaw @ 2022-09-11 19:32:58

试试double?


by _cyh0412_ @ 2022-09-11 19:36:41

float换成double


by break1 @ 2022-09-13 21:59:44

精度问题


by 13232169210yjh @ 2022-12-11 17:31:58

为什么是double而不是float啊,很烦啊


by Terry2011 @ 2023-06-01 14:21:31

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
    double ans=0.0;
    long long n,age,i;
    cin>>n;
    for(i=1; i<=n; i++) {
        cin>>age;
        ans+=age;
    }
    ans=ans/n;
    printf("%.2f",ans);
}

by lxplxplxplxp @ 2023-09-17 15:51:52

谢了,问题解决了


|