알고리즘/기타
[HackerRank] Encryption
히더
2018. 9. 30. 17:02
출처 : https://www.hackerrank.com/challenges/encryption/problem
#include <bits/stdc++.h>
using namespace std;
string encryption(string s) {
string ans;
int L = s.length(), row = sqrt(L), col = row + 1, it = 0;
if (row*row == L) col -= 1;
if (row*col < L) row += 1;
char arr[10][10]; memset(arr, 0, sizeof(arr));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col && it<L; j++) {
arr[i][j] = s.at(it);it++;
}
}
for (int j = 0; j < col; j++) {
for (int i = 0; i < row; i++) {
if (arr[i][j]) ans += arr[i][j];
}
if(j!=col-1)ans += " ";
}
return ans;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
string result = encryption(s);
fout << result << "\n";
fout.close();
return 0;
}