![[백준 / BOJ] 5622번 다이얼 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FvOBIK%2FbtrJj5dtrHM%2FetNBTi1uWuuC523kR3PCy1%2Fimg.png)
[백준 / BOJ] 5622번 다이얼 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 1. 17. 14:13
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/5622
5622번: 다이얼
첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어의 길이는 2보다 크거나 같고, 15보다 작거나 같다.
www.acmicpc.net
문제

문제 풀이
C++ 상세 풀이
C++ 풀이 펼쳐보기
아스키 코드에서 활용했던 개념을 이용하는 문제다. 각 문자에 매칭되는 숫자는 정해져 있으므로 아무래도 배열로 미리 값들을 선언해 두고 활용하는 것이 편할 것이다.
void solution(string str)
{
int num[] = {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 res = 0;
int len = str.length();
for (int i = 0; i < len; i++)
{
res += num[str[i] - 'A'];
}
cout << res;
}
Python 상세 풀이
Python 풀이 펼쳐보기
파이썬에는 if - in이라는 사기적인 구문이 존재해 리스트에 알파벳을 넣은 후 해결해도 된다.
alp = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']
text = input()
res = 0
for j in range(len(text)):
for i in alp:
if text[j] in i:
res += alp.index(i)+3
print(res)
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
void solution(string str) | |
{ | |
int num[] = {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 res = 0; | |
int len = str.length(); | |
for (int i = 0; i < len; i++) | |
{ | |
res += num[str[i] - 'A']; | |
} | |
cout << res; | |
} | |
int main() | |
{ | |
string str; | |
cin >> str; | |
solution(str); | |
return 0; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alp = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'] | |
text = input() | |
res = 0 | |
for j in range(len(text)): | |
for i in alp: | |
if text[j] in i: | |
res += alp.index(i)+3 | |
print(res) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1316번 그룹 단어 체커 (C++, Python) (0) | 2022.01.17 |
---|---|
[백준 / BOJ] 2941번 크로아티아 알파벳 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 2908번 상수 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 1152번 단어의 개수 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 1157번 단어 공부 (C++, Python) (0) | 2022.01.17 |
@Reo :: 코드 아카이브
자기계발 블로그