[BaekJoon] 2754번 - 학점계산 [Java][C++]
[BaekJoon] 2754번 - 학점계산 [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
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String grade = br.readLine();
double ans = 0;
if (grade.charAt(0) == 'A') {
ans += 4;
} else if (grade.charAt(0) == 'B') {
ans += 3;
} else if (grade.charAt(0) == 'C') {
ans += 2;
} else if (grade.charAt(0) == 'D') {
ans += 1;
} else {
System.out.println(ans);
return;
}
if (grade.charAt(1) == '+') {
ans += 0.3;
} else if (grade.charAt(1) == '-') {
ans -= 0.3;
}
System.out.println(ans);
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string grade;
cin >> grade;
double ans = 0;
if (grade.front() == 'A') {
ans += 4;
} else if (grade.front() == 'B') {
ans += 3;
} else if (grade.front() == 'C') {
ans += 2;
} else if (grade.front() == 'D') {
ans += 1;
} else {
cout << fixed << setprecision(1) << ans;
return 0;
}
if (grade.back() == '+') {
ans += 0.3;
} else if (grade.back() == '-') {
ans -= 0.3;
}
cout << fixed << setprecision(1) << ans;
}
This post is licensed under CC BY 4.0 by the author.