[백준] 25206번 - 너의 평점은 [Java][C++]
[백준] 25206번 - 너의 평점은 [Java][C++]
1. 문제 풀이
총 평점을 구하는 문제로 각 표에 맞춰 문자열을 실수 타입으로 변환하면 간단하게 해결할 수 있다.
2. 코드
1. 구현 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
double totalSum = 0;
double creditSum = 0;
for (int i = 0; i < 20; i++) {
st = new StringTokenizer(br.readLine());
st.nextToken();
double credit = Double.parseDouble(st.nextToken());
String grade = st.nextToken();
if (grade.equals("P")) continue;
if (grade.equals("A+")) {
totalSum += credit * 4.5;
} else if (grade.equals("A0")) {
totalSum += credit * 4.0;
} else if (grade.equals("B+")) {
totalSum += credit * 3.5;
} else if (grade.equals("B0")) {
totalSum += credit * 3.0;
} else if (grade.equals("C+")) {
totalSum += credit * 2.5;
} else if (grade.equals("C0")) {
totalSum += credit * 2.0;
} else if (grade.equals("D+")) {
totalSum += credit * 1.5;
} else if (grade.equals("D0")) {
totalSum += credit;
}
creditSum += credit;
}
System.out.println(totalSum / creditSum);
}
}
2. 구현 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double totalSum = 0;
double creditSum = 0;
for (int i = 0; i < 20; i++) {
string s, grade;
double credit;
cin >> s >> credit >> grade;
if (grade == "P") continue;
if (grade == "A+") {
totalSum += credit * 4.5;
} else if (grade == "A0") {
totalSum += credit * 4.0;
} else if (grade == "B+") {
totalSum += credit * 3.5;
} else if (grade == "B0") {
totalSum += credit * 3.0;
} else if (grade == "C+") {
totalSum += credit * 2.5;
} else if (grade == "C0") {
totalSum += credit * 2.0;
} else if (grade == "D+") {
totalSum += credit * 1.5;
} else if (grade == "D0") {
totalSum += credit;
}
creditSum += credit;
}
cout << totalSum / creditSum;
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 112 ms | 14400 KB | 1366 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2024 KB | 986 B |
This post is licensed under CC BY 4.0 by the author.