Baekjoon/단계별로 풀어보기

[BOJ/백준] 5622번 다이얼 C++ 문제 풀이

728x90

단계별로 풀어보기 - 문자열 - [8단계] 5622번

문제

문제 링크 : www.acmicpc.net/problem/5622

 

5622번: 다이얼

첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어는 2글자~15글자로 이루어져 있다.

www.acmicpc.net

입력 복사 : 

UNUCIC

 

CODE

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main() {
	string str;
	cin >> str;
	int num = 0;

	for (int i = 0; i < str.length(); i++) {
		if (str.at(i) >= 'A'&&str.at(i) <= 'C')
			num += 3;
		else if (str.at(i) >= 'D'&&str.at(i) <= 'F')
			num += 4;
		else if (str.at(i) >= 'G'&&str.at(i) <= 'I')
			num += 5;
		else if (str.at(i) >= 'J'&&str.at(i) <= 'L')
			num += 6;
		else if (str.at(i) >= 'M'&&str.at(i) <= 'O')
			num += 7;
		else if (str.at(i) >= 'P'&&str.at(i) <= 'S')
			num += 8;
		else if (str.at(i) >= 'T'&&str.at(i) <= 'V')
			num += 9;
		else if (str.at(i) >= 'W'&&str.at(i) <= 'Z')
			num += 10;
	}

	printf("%d", num);
}

 

결과

728x90