[알고리즘] 백준 5622, 11718

은개·2025년 2월 19일

[CS] 알고리즘

목록 보기
8/21

백준 5622 - 다이얼

정답

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <set>

using namespace std;

int main()
{
    string numbers;
    
    int cnt = 0;
    cin >> numbers;
    
    for (auto c : numbers) {
        if (c == 'A' || c == 'B' || c == 'C') { // 2
            cnt += 3;
        }
        else if (c == 'D' || c == 'E' || c == 'F') { // 3
            cnt += 4;
        }
        else if (c == 'G' || c == 'H' || c == 'I') { // 4
            cnt += 5;
        }
        else if (c == 'J' || c == 'K' || c == 'L') { // 5
            cnt += 6;
        }
        else if (c == 'M' || c == 'N' || c == 'O') { // 6
            cnt += 7;
        }
        else if (c == 'P' || c == 'Q' || c == 'R' || c == 'S') { // 7
            cnt += 8;
        }
        else if (c == 'T' || c == 'U' || c == 'V') { // 8
            cnt += 9;
        }
        else if (c == 'W' || c == 'X' || c == 'Y' || c == 'Z') { // 9
            cnt += 10;
        }
    }

    cout << cnt;
    return 0;
}

💡

  • 저렇게 문자 하나하나 조건문 처리 하는 대신 if (c == <'D'), if (c == <'G') 이렇게 하는 방법도 있음

백준 11718 - 그대로 출력하기

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <set>

using namespace std;

int main()
{
    string s;
    while (getline(cin, s)) {
        cout << s << endl;
    };

    return 0;
}

💡

  • 공백을 포함한 문자열을 입력 받을 때는 getline()을 사용

0개의 댓글