Algorithm/BOJ

[백준/C++17] 5622 다이얼

Logistic 2024. 11. 6. 01:08

문제


풀이

숫자 1을 걸라면 총 2초가 필요하다. 1보다 큰 숫자를 걸라면 1초씩 더 늘어나기 때문에 시작은 3초부터. 

알파벳 별로 (ABC는 3초 , DEF는 4초) 시간을 배열에 저장 후 (문자 - 'A')로 인덱스 접근하여 ans에 시간을 더해주면 정답.


코드

#include <iostream>

using namespace std;

int main() {
    int time[26] = { 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10 };
    int ans = 0;
    string str;
    cin >> str;
    for(int i = 0; i< str.length();i++) {
        ans += time[str[i]-'A'];
    }
    cout << ans;
    return 0;
}