백준 단계별
[Java] 백준 4344: 평균은 넘겠지
pullwall
2023. 1. 11. 15:05
728x90
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] score;
int test=sc.nextInt();
for(int i=0;i<test;i++) {
int N = sc.nextInt();
score = new int[N];
double sum =0;
for(int j=0;j<N;j++) {
int val= sc.nextInt();
score[j]= val;
sum+=val;
}
double avg = sum/N;
double cnt=0;
for(int k=0;k<N;k++) {
if(avg<score[k]) {
cnt++;
}
}
double ratio = (cnt/N)*100;
System.out.printf("%.3f%%\n", ratio);
}
}
}
문제 티어에 비해 알고리즘은 비교적 단순하다.
다만 잊고 있었던 반올림하여 소수점 N번째까지 출력하기를 상기시킬 수 있는 문제였다.
또한 printf에서 %는 형식지정자로 타입을 지정하는 특수 문자이다. 그렇기 때문에 % 단독으로는 %문자를 출력할 수가 없고 %%을 써주어야 %문자가 출력된다.
728x90