Coding Test/BaekJoon

[BaekJoon Java] 평균은 넘겠지(4344)

momong'-' 2020. 12. 13. 18:55

www.acmicpc.net/problem/4344

 

4344번: 평균은 넘겠지

대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.

www.acmicpc.net


  • 문제

대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.

 

  • 예제 입출력

  • 풀이
import java.util.Scanner;

/**
 * 대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.
 * 첫째 줄에는 테스트 케이스의 개수 C가 주어진다.
 * 둘째 줄부터 각 테스트 케이스마다 학생의 수 N(1 ≤ N ≤ 1000, N은 정수)이 첫 수로 주어지고, 이어서 N명의 점수가 주어진다. 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.
 * 각 케이스마다 한 줄씩 평균을 넘는 학생들의 비율을 반올림하여 소수점 셋째 자리까지 출력한다.
 * 
 * @author imj10
 *
 */
public class AboveAverage_4344 {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int caseCnt = 0;
		
		caseCnt = sc.nextInt();
		
		
		for ( int i=0; i < caseCnt; i++ ) {
			
			int studentCnt = 0;
			studentCnt = sc.nextInt();
			
			int totalScore = 0;
			double avg = 0;
			int aboveAvgCnt = 0;

			int [] scoreArray = new int[studentCnt];
			
			for ( int j=0; j < studentCnt; j++ ) {
				scoreArray[j] = sc.nextInt();
				
				totalScore += scoreArray[j];
			}
			
			avg = totalScore/studentCnt;
			
			for ( int j=0; j < studentCnt; j++ ) {
				if ( scoreArray[j] > avg ) {
					aboveAvgCnt ++;
				}
			}
			System.out.println(String.format("%.3f", (double)aboveAvgCnt/studentCnt * 100) + "%");
		}
	}
}
 
  • 결과


  • 추가설명

반올림 방법

 

Math.round()와 String.format()차이점

 

double num = 100.000;

 

String.format("%.3f", num) // 100.000

Math.round(num*1000)/1000) // 100