-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.java
More file actions
38 lines (30 loc) · 826 Bytes
/
encryption.java
File metadata and controls
38 lines (30 loc) · 826 Bytes
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//solution to: https://www.hackerrank.com/challenges/encryption
public class encryption {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
double length = str.length();
double x = Math.sqrt(length);
int columns = (int)Math.ceil(x);
int rows = (int)Math.floor(x);
int dim = columns*rows;
while(dim < length){
rows = rows +1;
dim = columns*rows;
}
char[] charArray = str.toCharArray();
StringBuilder output = new StringBuilder();
for(int i = 0; i < columns; i++){
for(int j = i; j < length; j+=columns){
output.append(charArray[j]);
}
output.append(" ");
}
System.out.println(output);
}
}