2-05. Calculate the mean variance of the Set Data (15) (zjupat mathematics)

Source: Internet
Author: User

Link: http://pat.zju.edu.cn/contests/ds/2-05


The design function calculates the mean variance of n given integers. If the average value of N numbers a [] is recorded as AVG, the formula for calculating the mean variance is:

Input format description:

The first line is a positive integer of N (<= 1st) and the second line is an integer of N.

Output format description:

The average variance of N numbers is output, which requires a fixed precision to output the 5 digits after the decimal point.

Sample input and output:

Serial number Input Output
1
106 3 7 1 4 8 2 9 11 5
3.03974
2
12
0.00000


The Code is as follows:

#include <cstdio>#include <cmath>int main(){    int a[10017];    double avg;    int n;    while(~scanf("%d",&n))    {        double sum = 0, ans = 0;        for(int i = 0; i < n; i++)        {            scanf("%d",&a[i]);            sum+=a[i];        }        avg = sum/(n*1.0);        sum = 0;        for(int i = 0; i < n; i++)        {            sum+=(a[i]-avg)*(a[i]-avg);        }        sum/=(n*1.0);        ans = sqrt(sum);        printf("%.5lf\n",ans);    }    return 0;}


2-05. Calculate the mean variance of the Set Data (15) (zjupat mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.