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)