알고리즘 문제

[백준] 4246번 To and Fro

feelcoding 2020. 2. 24. 19:19
728x90

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

 

4246번: To and Fro

Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example,

www.acmicpc.net

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

int main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0) break;
		string s;
		cin >> s;
		vector<vector<char>> arr(s.size() / n, vector<char>(n));
		int index = 0;
		for (int i = 0; i < s.size() / n; i++) {
			if (i % 2 == 0) {
				for (int j = 0; j < n; j++) {
					arr[i][j] = s[index++];
				}
			}
			else {
				for (int j = n - 1; j >= 0; j--) {
					arr[i][j] = s[index++];
				}
			}
		}
		for (int j = 0; j < n; j++) {
			for (int i = 0; i < s.size() / n; i++) {
				cout << arr[i][j];
			}
		}
		cout << '\n';
	}
	return 0;
}
728x90