import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
int N = scanner.nextInt();
if(N == 0) break; //未考虑
int[] numbers = new int[N];
int sum = 0; //数列元素的总和
for (int i = 0; i < N; i++) {
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
//计算平均值
double average = (double) sum / N;
//平方和:每个元素与平均值的差的平方和
double varianceSum = 0; // 差的平方的总和
for (int number : numbers) {
varianceSum += Math.pow(number-average,2);
}
//计算平均方差
double variance = varianceSum / N;
// 输出平均方差的整数部分(向下取整)
System.out.println((int)Math.floor(variance));
}
}
}
求解平均方差
最新推荐文章于 2024-08-16 21:01:11 发布