https://www.acmicpc.net/problem/25206
Answer
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
final int INPUT_LINE = 20;
double creditSum = 0.0;
double gpaSum = 0.0;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
for (int i = 0; i < INPUT_LINE; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String subjectName = st.nextToken();
double credit = Double.parseDouble(st.nextToken());
String grade = st.nextToken();
if (!"P".equals(grade)) {
gpaSum += calculateGpa(credit, grade);
creditSum += credit;
}
}
} catch (IOException e) {
System.err.println("ERROR = " + e.getMessage());
}
System.out.printf("%.6f", gpaSum / creditSum);
}
static double calculateGpa(double credit, String grade) {
double gradePoint = 0.0;
switch (grade) {
case "A+":
gradePoint = 4.5;
break;
case "A0":
gradePoint = 4.0;
break;
case "B+":
gradePoint = 3.5;
break;
case "B0":
gradePoint = 3.0;
break;
case "C+":
gradePoint = 2.5;
break;
case "C0":
gradePoint = 2.0;
break;
case "D+":
gradePoint = 1.5;
break;
case "D0":
gradePoint = 1.0;
break;
case "F":
gradePoint = 0.0;
break;
}
return credit * gradePoint;
}
}
Code Review
final int INPUT_LINE = 20;
double creditSum = 0.0;
double gpaSum = 0.0;
문제에서 입력 지문에 20줄이 주어진다고 하여 `INPUT_LINE`을 선언했다.
학점의 총합을 위한 `creditSum`과 전공과목별 (학점 x 과목평점)의 합을 위한 `gpaSum`을 선언했다.
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
...
} catch (IOException e) {
System.err.println("ERROR = " + e.getMessage());
}
`try-with-resources`로 try에 객체의 자원을 받아서 try문이 끝나면 자원을 반환한다.
`System.in`으로 사용자로부터 입력받은 데이터를 바이트 스트림으로 받는다.
`InputStreamReader` 클래스로 바이트 스트림을 문자 기반 스트림으로 변환한다.
`BufferedReader` 클래스로 문자 스트림을 읽는다.
for (int i = 0; i < INPUT_LINE; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String subjectName = st.nextToken();
double credit = Double.parseDouble(st.nextToken());
String grade = st.nextToken();
if (!"P".equals(grade)) {
gpaSum += calculateGpa(credit, grade);
creditSum += credit;
}
}
입력 문자열을 INPUT_LINE 20줄까지 읽기 위해 for문으로 한 문자열씩 로직을 반복한다.
`StringTokenizer` 클래스로 문자열 한 행을 공백 구분자로 나눈다.
`nextToken()` 메서드로 구분된 문자열을 각각 `subjectName` 과목명, `credit` 학점, `grade` 등급 변수로 생성한다.
지문에서 P인 과목은 계산에서 제외해야 하므로 현재 `grade`가 P일 경우를 제외한다.
마지막에 전공평점을 구하기 위해 `calculateGpa()` 메서드의 값을 `gpaSum`에 더하고, `credit`의 값을 `creditSum`에 계속 더한다.
static double calculateGpa(double credit, String grade) {
double gradePoint = 0.0;
switch (grade) {
case "A+":
gradePoint = 4.5;
break;
case "A0":
gradePoint = 4.0;
break;
case "B+":
gradePoint = 3.5;
break;
case "B0":
gradePoint = 3.0;
break;
case "C+":
gradePoint = 2.5;
break;
case "C0":
gradePoint = 2.0;
break;
case "D+":
gradePoint = 1.5;
break;
case "D0":
gradePoint = 1.0;
break;
case "F":
gradePoint = 0.0;
break;
}
return credit * gradePoint;
}
`switch-case`문으로 등급에 따른 과목평점을 구한다.
전공과목별 (학점 x 과목평점)을 반환한다.
System.out.printf("%.6f", gpaSum / creditSum);
위의 로직 진행 후 전공과목별 (학점 x 과목평점)의 합인 `gpaSum`을 학점의 총합인 `creditSum`으로 나누어 전공평점을 구하고 `%.6f`로 소수점 6번째까지 출력한다.
Result