西北工業大學 校賽 隨機序列 題解

題目

在這裏插入圖片描述

思路

數據量不大,直接用類存儲每一個卷子。直接算就是了。這道題的問題在於很多人沒法控制格式化輸出。其實很簡單,他題目沒說,但可以觀察出是四捨五入到小數後第三位,直接System.out.println("%.3f",%float) 就完事
import java.util.*;
public class 測試{
    public static class paper{
        int  number;
        int []list;
        public paper(int number){
            this.number=number;this.list=new int[number];
        }
        
    }
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        paper [] paperList=new paper[n];
        for(int i =0;i<n;i++) {
        	int number=sc.nextInt();
        	paperList[i]=new paper(number);
        	for(int j =0;j<number;j++) {
        		paperList[i].list[j]=sc.nextInt();
        	}
        }
        for(int i =0;i<n;i++){
            getJiCha(paperList[i]);
        }
    }
    public static void getJiCha(paper p){
        Arrays.sort(p.list);
        System.out.print((p.list[p.list.length-1]-p.list[0])+" " );
        getFangcha(p);
    }
    public static void getFangcha(paper p){
        double  av=0;
        for(int i = 0;i<p.list.length;i++){
            av+=p.list[i];
        }
        av/=p.list.length;
        double sum=0;
        for(int i=0;i<p.list.length;i++){
           double tmp=  Math.pow(p.list[i]-av,2);
           sum+=tmp;
        }
        sum/=p.list.length;
        System.out.printf("%.3f\n",sum);
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章