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

'알고리즘 문제' 카테고리의 다른 글

[백준] 2522번 별 찍기 - 12  (0) 2020.02.24
[백준] 3184번 양  (0) 2020.02.24
[백준] 4101번 크냐?  (0) 2020.02.24
[백준] 1388번 바닥 장식  (0) 2020.02.24
[백준] 1075번 나누기  (0) 2020.02.24

+ Recent posts