-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareCode.java
More file actions
61 lines (46 loc) · 1.25 KB
/
Copy pathSquareCode.java
File metadata and controls
61 lines (46 loc) · 1.25 KB
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
58
59
60
61
import java.util.Scanner;
public class SquareCode
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String code = in.next();
//floor(sqrt( len(word) )) <= width, height <= ceil(sqrt( len(word) ))
int min = (int) Math.floor(Math.sqrt((code.length())));
int max = (int) Math.ceil(Math.sqrt((code.length())));
// System.out.println(min + " " + max);
if(min + max < code.length())
min += 1;
char[][] x = new char[min][max];
char[] c = code.toCharArray();
int index = 0;
for(int i = 0; i < min; i++)
for(int j = 0; j < max; j++)
{
if(index >= c.length)
break;
x[i][j] = c[index];
index++;
}
// debug=================================
// for(int i = 0; i < min; i++)
// {
// for(int j = 0; j < max; j++)
// {
// System.out.print(x[i][j]);
// }
// System.out.println(" ");
// }
// =======================================
for(int i = 0; i < max; i++)
{
for(int j = 0; j < min; j++)
{
if(x[j][i] == '\u0000')
continue;
System.out.print(x[j][i]);
}
System.out.print(" ");
}
}
}