728x90
암호화된 문자를 원래 문자로 바꾸는 문제입니다.
암호화하는걸 반대로 복호화해주는 식으로 풀어주면 됩니다.
먼저 왼쪽->오른쪽, 오른쪽->왼쪽 으로 읽어서 만들어진 문자를 다시 n열의 배열모양으로 바꿔줍니다.
그리고 배열을 세로로 읽으면 원래 문자가 나옵니다.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int n;
int c = 0;
int rem;
int cycle;
bool flag = false;
char map[20][200];
string s;
string result = "";
cin >> n;
cin >> s;
memset(map, ' ', sizeof(map));
cycle = (s.length() + n - 1) / n;
for (int i = 0; i < cycle; ++i) {
if (!flag) {
for (int j = 0; j < n; ++j) {
map[i][j] = s[c++];
}
}
else {
for (int j = n - 1; j >= 0; --j) {
map[i][j] = s[c++];
}
}
flag = !flag;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < cycle; ++j) {
result += map[j][i];
}
}
cout << result << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 2037번 문자메시지 (0) | 2019.11.05 |
---|---|
[C++] 백준 5671번 호텔 방 번호 (0) | 2019.11.05 |
[C++] 백준 14710번 고장난 시계 (0) | 2019.11.05 |
[C++] 백준 4659번 비밀번호 발음하기 (0) | 2019.11.05 |
[C++] 백준 1996번 지뢰 찾기 (0) | 2019.11.04 |