알고리즘/백준
4344번 문제 : 평균은 넘겠지
son_i
2023. 4. 25. 17:46
728x90
4344번: 평균은 넘겠지
대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.
www.acmicpc.net
문제는 매우 간단했으나 소수점 아래 n번째 자리까지 출력하기를 새로 알아서 써본다 !
방법은 2가지
1. String.format()
2. Math.round()
1번 String.format()을 이용하면
double a = 1.23456
System.out.println(".3%f",a);
하면 소수점 넷째자리에서 반올림되어 3번 째 자리까지 표시된다.
2번 Math.round() 이용
Math.round()는 반올림하는 함수인데 2째자리에서 반올림해서 첫쨰자리까지만 나타내고 싶다면
double a = 1.23456
System.out.println(Math.round(a*10)/10.0);
a에 10 곱해서 반올림해준다음에 다시 10.0으로 나눠주면 됨
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0;i<num;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int count = Integer.parseInt(st.nextToken());
int score[] = new int[count];
int sum = 0;
for(int j = 0;j<count;j++) {
score[j] = Integer.parseInt(st.nextToken());
sum += score[j];
}
double avg = sum/count;
double high = 0;
for(int j=0;j<count;j++) {
if(score[j] > avg) {
high ++;
}
}
sb.append(String.format("%.3f",high/count*100)+"%\n");
}
System.out.println(sb);
}
}
728x90