-
Notifications
You must be signed in to change notification settings - Fork 0
/
tajna.cpp
57 lines (47 loc) · 1.1 KB
/
tajna.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
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Get the string
string s;
cin >> s;
// Get the length
int n = s.size();
// Must be at least as small as the sqrt of the length for r <= c
int r = floor(sqrt(n));
// Decrement until r is a factor of n
while(n % r != 0)
{
r--;
}
// c must be the other dimension to give n
int c = n / r;
// Create the matrix to store the characters
char **mat = new char*[r];
for(int i = 0; i < r; i++)
{
mat[i] = new char[c];
}
int x = 0; // Track which character should be inserted into the matrix next
// Loop over the columns and rows
for(int i = 0; i < c; i++)
{
for(int j = 0; j < r; j++)
{
// Insert the character into the matrix
mat[j][i] = s[x];
x++;
}
}
// Loop over the rows and columns
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
// Print out the character
cout << mat[i][j];
}
}
cout << endl;
return 0;
}