[2754] 학점계산

sky·2022년 8월 4일
0

BaekJoon Online Judge(B)

목록 보기
54/98
post-thumbnail

문제

Bronze Ⅴ

어떤 사람의 C언어 성적이 주어졌을 때, 평점은 몇 점인지 출력하는 프로그램을 작성하시오.

  • A+: 4.3, A0: 4.0, A-: 3.7
  • B+: 3.3, B0: 3.0, B-: 2.7
  • C+: 2.3, C0: 2.0, C-: 1.7
  • D+: 1.3, D0: 1.0, D-: 0.7
  • F: 0.0

입력
첫째 줄에 C언어 성적이 주어진다. 성적은 문제에서 설명한 13가지 중 하나이다.

출력
첫째 줄에 C언어 평점을 출력한다.


Solution

C++

#include <iostream>
using namespace std;

int main() {
    string str;
    cin >> str;
    if(str == "A+") cout << "4.3";
    if(str == "A0") cout << "4.0";
    if(str == "A-") cout << "3.7";
    if(str == "B+") cout << "3.3";
    if(str == "B0") cout << "3.0";
    if(str == "B-") cout << "2.7";
    if(str == "C+") cout << "2.3";
    if(str == "C0") cout << "2.0";
    if(str == "C-") cout << "1.7";
    if(str == "D+") cout << "1.3";
    if(str == "D0") cout << "1.0";
    if(str == "D-") cout << "0.7";
    if(str == "F") cout << "0.0";
    return 0;
}

출력문에 단순 " "없이 단순 숫자만 적으면 틀린다. 소수점 표시가 안 되기 때문이다.

Another Solution

#include <iostream>
using namespace std;
int main(void)
{

	string str;
	cin >> str;
	double res=0;
	cout << fixed;
	cout.precision(1);

	switch (str[0])
	{
	case 'A' :
		res += 4.0;
		break;
	case 'B':
		res += 3.0;
		break;
	case 'C':
		res += 2.0;
		break;
	case 'D':
		res += 1.0;
		break;
	}	
	switch (str[1])
	{
	case '+':
		res += 0.3;
		break;
	case '-':
		res -= 0.3;
		break;
	}
	cout << res;
}

case문도 있다는 걸 생각못했다. 세련되고 좋은 코드인 것 같다.
출처 : https://non-stop.tistory.com/41


Total Time

  • 2022-08-05 | 01:35 - 01:50 Success!
profile
개발자가 되고 싶은 1人

0개의 댓글

관련 채용 정보