题解 | 牛牛的数学作业
牛牛的数学作业
https://www.nowcoder.com/practice/6f5d239c34a7429cb325a3d836abc342
#include <math.h>
#include <stdio.h>
int main() {
int T;
scanf("%d",&T);
for (int t= 0; t<T; t++){
int N;
scanf("%d",&N);
int string[N];
for (int i= 0; i<N; i++) {
int num=0;
scanf("%d",&string[i]);
}
int max=string[0];int min=string[0];
int sum = 0;
for (int i= 0; i<N; i++)
{
sum+=string[i];
if (string[i]>=max) {
max=string[i];
}
if (string[i]<=min) {
min=string[i];
}
}
float avg = (float)sum / N;
int range = max -min;
double sum1 = 0.0;
for (int i=0; i<N; i++) {
sum1+=pow((double)(string[i]-avg), 2)/N;
}
printf("%d %.3f\n", range, sum1);
}
return 0;
}
