백준 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') {
cnt += 3;
}
else if (c == 'D' || c == 'E' || c == 'F') {
cnt += 4;
}
else if (c == 'G' || c == 'H' || c == 'I') {
cnt += 5;
}
else if (c == 'J' || c == 'K' || c == 'L') {
cnt += 6;
}
else if (c == 'M' || c == 'N' || c == 'O') {
cnt += 7;
}
else if (c == 'P' || c == 'Q' || c == 'R' || c == 'S') {
cnt += 8;
}
else if (c == 'T' || c == 'U' || c == 'V') {
cnt += 9;
}
else if (c == 'W' || c == 'X' || c == 'Y' || c == 'Z') {
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()을 사용