※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 9783번 문제인 Easy Encryption이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/9783 

 

9783번: Easy Encryption

In order to send a secret message to your friend, you invent an original method to encrypt the message. The encryption rule is quite simple. A letter is replaced with 2-digit number. Each letter has its corresponding number as the following: a = 01, b = 02

www.acmicpc.net

주어지는 한 줄의 문자열(공백 포함 가능)의 문자들을 조건에 맞게 바꾸어 출력해주는 문제이다.

 

각 문자는 아스키 코드, 즉 정수로 생각할 수 있음을 이용하면 적절한 수식과 조건문을 통해 문제를 간단히 해결할 수 있을 것이다.

 

getline을 이용해 줄단위의 문자열을 읽을 수 있음을 상기하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <string>
using namespace std;

string s;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	getline(cin, s);

	for (auto& l : s) {
		if ('a' <= l && l <= 'z') {
			int ret = l - 'a' + 1;
			if (ret < 10) cout << 0;
			cout << ret;
		}
		else if ('A' <= l && l <= 'Z') {
			int ret = l - 'A' + 27;
			cout << ret;
		}
		else if ('0' <= l && l <= '9') cout << '#' << l;
		else cout << l;
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 9773 // C++] ID Key  (0) 2023.02.14
[BOJ 9782 // C++] Median  (0) 2023.02.14
[BOJ 16171 // C++] 나는 친구가 적다 (Small)  (0) 2023.02.14
[BOJ 27465 // C++] 소수가 아닌 수  (0) 2023.02.14
[BOJ 2346 // C++] 풍선 터뜨리기  (0) 2023.02.14

+ Recent posts