-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEncryption.cpp
45 lines (36 loc) · 897 Bytes
/
Encryption.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
// Complete the encryption function below.
string encryption(string s) {
string temp = "", result = "";
for(int i = 0; i < s.length(); ++i) {
if(s[i] != ' ') temp += s[i];
}
int L = temp.length();
double rootL = sqrt(L);
int rows = floor(rootL), cols = ceil(rootL);
if(rows * cols < L) ++rows;
int l = 0;
string mat[cols];
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
if(l >= L) {
break;
}
mat[j] += temp[l++];
}
}
for(int i = 0; i < cols; ++i) result += mat[i] + ' ';
result += '\n';
return result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
string result = encryption(s);
fout << result << "\n";
fout.close();
return 0;
}