close

題目概要:

找出分數在平均以上佔多少百分比。

解題方向:

先計算出平均分數後,計算有多少分數是在平均以上,再去計算百分比。

程式碼:

//Java
import java.util.Scanner;
class uva10370{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=sc.nextInt();
for(int i=0;i<cases;i++){
int n=sc.nextInt();
int grade[]=new int[n];
for(int j=0;j<n;j++){
grade[j]=sc.nextInt();
}
//計算平均分數
int total=0;
for(int j=0;j<n;j++) total=total+grade[j];
int average=total/n;
//計算百分比,並輸出
float count=0;
for(int j=0;j<n;j++) if(grade[j]>average) count++;
System.out.printf("%.3f%%",count/n*100);
System.out.println("");
}
}
}
view raw uva10370.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()