본문 바로가기
Problem Solving/백준

[백준 알고리즘] 1546: 평균(자바)

by 청량리 물냉면 2021. 8. 1.
반응형
문제

https://www.acmicpc.net/problem/1546

 

1546번: 평균

첫째 줄에 시험 본 과목의 개수 N이 주어진다. 이 값은 1000보다 작거나 같다. 둘째 줄에 세준이의 현재 성적이 주어진다. 이 값은 100보다 작거나 같은 음이 아닌 정수이고, 적어도 하나의 값은 0보

www.acmicpc.net

 

 

코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt(); //과목 수
double sum = 0; //시험점수 총점
double[] score = new double[test]; //과목 수만큼 배열 생성
for(int i = 0; i < test; i++) {
score[i] = sc.nextInt();
}
double M = score[0]; //점수 중 최댓값 구하기
for(int i = 1; i < test; i++) {
if(score[i] > M)
M = score[i];
}
for(int i = 0; i < test; i++) {
score[i] = score[i] / M * 100; //점수 고치기
sum += score[i]; //고친 점수를 모두 더한 값
}
System.out.println(sum / test); //점수의 합을 과목 갯수로 나누기
sc.close();
}
}

 

 

마무리

배열 + 최댓값 구하기 조금 응용한 문제인듯. 수월하게 풀었다. 


 

 

반응형