헬린코린이

[백준] 1546 : 평균 - JAVA 본문

Programming/Coding Test Practice

[백준] 1546 : 평균 - JAVA

HCD 2023. 3. 29. 22:03

 

주의 : 출력이 .0이므로 배열을 실수형으로 선언해야 함!

어려웠던 점 : examTotal을 int로 해서 System.out.println()에서 (double)로 형변환해 줘서 출력을 해도 예제 2번의 출력값이

                    동일하게 나오지 않았습니다. double로 해서 해결 

이유 : 20을 30으로 나누고 100을 곱한 값이 int로 들어가서 일 것 같음

import java.util.Scanner;

public class Main{
   	public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double[] exam = new double[sc.nextInt()];
        int maxExamNum = 0;
        double examTotal = 0;
        for(int i=0; i<exam.length; i++){
            exam[i] = sc.nextInt();
            if(maxExamNum < exam[i]) maxExamNum = (int)exam[i];
        }
       for(int i=0; i<exam.length; i++) {
    	   examTotal+= exam[i]/maxExamNum*100;
       }
       System.out.println(examTotal/exam.length);
    }
}

 

Comments