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

 

이번에 볼 문제는 백준 11288번 문제인 Ether's Encryption이다.
문제는 아래 링크를 확인하자.

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

 

11288번: Ethel’s Encryption

The first line contains three integers n, a, and b. n is the number of characters in the encrypted message, including spaces. The numbers a and b are used to calculate the offset ab, with 0 ≤ a ≤ 231 and 0 ≤ b ≤ 216, however at least a or b will be

www.acmicpc.net

주어진 문자열의 각 대문자들을(즉, ' '를 제외한 모든 문자들을) \(a^b\)만큼 shift시키는 것으로 문제를 해결하자.

 

이 shift연산은 26을 주기로 반복되므로 \(a^b\) 대신 \(a^b%26\)을 계산하는 것으로도 문제를 충분히 해결할 수 있다. 이 값은 \(a\) 대신 \(a%26\)을 이용하고 \(a\)를 반복해 곱해나가는 과정에서도 그 결과를 26으로 나눈 나머지를 저장하는 것으로 계산해낼 수 있다.

 

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

#include <iostream>
#include <string>
using namespace std;
typedef long long ll;

int slen; ll a, b, d = 1;
string s;

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

	cin >> slen >> a >> b;
	a %= 26;
	for (int k = 0; k < b; k++) {
		d = (d * a) % 26;
	}

	getline(cin, s);
	getline(cin, s);
	for (auto& l : s) {
		if (l == ' ') cout << ' ';
		else {
			char nxt = l - d;
			if (nxt < 'A') nxt += 26;
			cout << nxt;
		}
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 11290 // C++] Wonowon  (0) 2022.12.13
[BOJ 11287 // C++] Margaret’s Minute Minute Manipulation  (0) 2022.12.13
[BOJ 26307 // C++] Correct  (0) 2022.12.13
[BOJ 5212 // C++] 지구 온난화  (0) 2022.12.13
[BOJ 11289 // C++] Boolean Postfix  (0) 2022.12.13

+ Recent posts