import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
scan.nextLine();
for (int i =0; i<count; i++) {
String test = scan.nextLine();
String[] testarr = test.split(" ");
int studentcnt = Integer.parseInt(testarr[0]);
int[] scorearr = new int[studentcnt];
for (int j=1; j<=studentcnt; j++){
scorearr[j-1] = Integer.parseInt(testarr[j]);
}
int sumscore = 0;
for (int j=0; j<studentcnt; j++){
sumscore += scorearr[j];
}
int avgscore = sumscore / studentcnt;
int avgstudent = 0;
for (int j=0; j<studentcnt; j++){
if ( scorearr[j] > avgscore) {
avgstudent += 1;
}
}
float avgstudentcnt = (float)avgstudent / (float)studentcnt;
System.out.println(String.format("%.3f%%",avgstudentcnt*100));
}
scan.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr;
int testcase = in.nextInt();
for(int i = 0 ; i < testcase ; i++) {
int N = in.nextInt(); //학생 수
arr = new int[N];
double sum = 0; // 성적 누적 합 변수
// 성적 입력부분
for(int j = 0 ; j < N ; j++) {
int val = in.nextInt(); // 성적 입력
arr[j] = val;
sum += val; // 성적 누적 합
}
double mean = (sum / N) ;
double count = 0; // 평균 넘는 학생 수 변수
// 평균 넘는 학생 비율 찾기
for(int j = 0 ; j < N ; j++) {
if(arr[j] > mean) {
count++;
}
}
System.out.printf("%.3f%%\n",(count/N)*100);
}
in.close();
}
}
차이점
int count = scan.nextInt(); //"5"입력
scan.nextLine(); //"Enter" 입력
for (int i =0; i<count; i++) {
String test = scan.nextLine(); // "5 50 50 70 80 100"
String[] testarr = test.split(" ");
String[] testarr = test.split(" ");
int studentcnt = Integer.parseInt(testarr[0]);
int[] scorearr = new int[studentcnt];
for (int j=1; j<=studentcnt; j++){
scorearr[j-1] = Integer.parseInt(testarr[j]);
}
float avgstudentcnt = (float)avgstudent / (float)studentcnt;
변수를 선언할때 자료형 뿐만 아니라 입력값도 float으로 선언해주어야 한다.