본문 바로가기

Coding Test/PS

[백준/Java] 10039번: 평균 점수 (브론즈4)

🟤 백준 10039번: 평균 점수 (브론즈4)

 

문제 풀이

1차 풀이

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int total = 0;
		
		for (int i=0; i<5; i++) {
                    int s = Integer.parseInt(br.readLine());

                    if (s < 40) {
                        s = 40; // 40점 미만은 40점으로 고정
                    }

                    total += s;    // 모든 점수 더하기
		}
		
		System.out.println(total/5);
	}
}

 

다른 풀이

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int total = 0;
		
		for (int i=0; i<5; i++) {
                    int s = Integer.parseInt(br.readLine());
                    total += Math.max(40, s);
		}
		
		System.out.println(total/5);
	}
}

 

조건문 대신 `Math.max(40, s)`로 40점 미만인 점수를 40점으로 보정