题解 | 牛牛的数学作业

牛牛的数学作业

https://www.nowcoder.com/practice/6f5d239c34a7429cb325a3d836abc342

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        for(int i = 0;i<n;i++){
            int m = in.nextInt();
            int[] a = new int[m];
            int min;
            int max;
            int sum = 0;
            double avr;
            for(int p = 0;p<m;p++){
                a[p] = in.nextInt();
                sum+=a[p];
            }
            avr = sum*1.0/m;
            min = max = a[0];
            double su = 0.0;
            double f;
            for(int p = 0;p<m;p++){
                if(a[p]>max){
                    max = a[p];
                }
                if(a[p]<min){
                    min = a[p];
                }
                su+= (a[p]-avr)*(a[p]-avr);
            }
            int s = max-min;
            double s1 = su/m;
            System.out.print(s+" ");
            System.out.printf("%.3f%n",s1);
        }
    }
}

全部评论
点赞 回复 分享
发布于 01-24 21:00 山东

相关推荐

Edgestr:没项目地址就干脆把那一栏删了呗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务