티스토리 뷰

알고리즘/기타

[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;

}



'알고리즘 > 기타' 카테고리의 다른 글

[HackerRank] Organizing Containers of Balls  (0) 2018.10.01
[HackerRank] Queen's Attack II  (0) 2018.09.30
[HackerRank] Non-Divisible Subset  (0) 2018.09.30
[HackerRank] Forming a Magic Square  (0) 2018.09.30
[HackerRank] Extra Long Factorials  (0) 2018.09.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함