/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// Example usage
String[] input
= {"1",
"2",
"3",
"x",
"5",
"6",
"a",
"baaaaaaasdfasdf",
"c",
"10",
"11", "12", "13", "14", "15", "16"};
int numCols = 5; // Fixed number of columns
int rows
= (int) Math.
ceil((double) input.
length / numCols
); // Calculate number of rows int[] maxColWidths = new int[numCols]; // Array to store max widths for each column
// Step 1: Calculate the max width for each column
for (int i = 0; i < input.length; i++) {
int colIndex = i % numCols;
maxColWidths
[colIndex
] = Math.
max(maxColWidths
[colIndex
], input
[i
].
length()); }
// Step 2: Print the table with the proper spacing
for (int i = 0; i < rows; i++) {
StringBuilder line = new StringBuilder();
for (int j = 0; j < numCols; j++) {
int index = i * numCols + j;
if (index < input.length) {
line.append(input[index]); // Append the string in the cell
// Calculate and add the padding
if (j < numCols - 1) { // No padding needed after the last column
int spaces = maxColWidths[j] - input[index].length() + 4; // 4 spaces between columns
for (int k = 0; k < spaces; k++) {
line.append(" ");
}
}
}
}
System.
out.
println(line.
toString().
trim()); // Print the line }
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoKICAgICAgICAvLyBFeGFtcGxlIHVzYWdlCiAgICAgICAgU3RyaW5nW10gaW5wdXQgPSB7IjEiLCAiMiIsICIzIiwgIngiLCAiNSIsICI2IiwgImEiLCAiYmFhYWFhYWFzZGZhc2RmIiwgImMiLCAiMTAiLCAKICAgICAgICAgICAgICAgICAgICAgICAgICAiMTEiLCAiMTIiLCAiMTMiLCAiMTQiLCAiMTUiLCAiMTYifTsKCiAgIGludCBudW1Db2xzID0gNTsgLy8gRml4ZWQgbnVtYmVyIG9mIGNvbHVtbnMKICAgICAgICBpbnQgcm93cyA9IChpbnQpIE1hdGguY2VpbCgoZG91YmxlKSBpbnB1dC5sZW5ndGggLyBudW1Db2xzKTsgLy8gQ2FsY3VsYXRlIG51bWJlciBvZiByb3dzCiAgICAgICAgaW50W10gbWF4Q29sV2lkdGhzID0gbmV3IGludFtudW1Db2xzXTsgLy8gQXJyYXkgdG8gc3RvcmUgbWF4IHdpZHRocyBmb3IgZWFjaCBjb2x1bW4KCiAgICAgICAgLy8gU3RlcCAxOiBDYWxjdWxhdGUgdGhlIG1heCB3aWR0aCBmb3IgZWFjaCBjb2x1bW4KICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGlucHV0Lmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIGludCBjb2xJbmRleCA9IGkgJSBudW1Db2xzOwogICAgICAgICAgICBtYXhDb2xXaWR0aHNbY29sSW5kZXhdID0gTWF0aC5tYXgobWF4Q29sV2lkdGhzW2NvbEluZGV4XSwgaW5wdXRbaV0ubGVuZ3RoKCkpOwogICAgICAgIH0KCiAgICAgICAgLy8gU3RlcCAyOiBQcmludCB0aGUgdGFibGUgd2l0aCB0aGUgcHJvcGVyIHNwYWNpbmcKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHJvd3M7IGkrKykgewogICAgICAgICAgICBTdHJpbmdCdWlsZGVyIGxpbmUgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG51bUNvbHM7IGorKykgewogICAgICAgICAgICAgICAgaW50IGluZGV4ID0gaSAqIG51bUNvbHMgKyBqOwogICAgICAgICAgICAgICAgaWYgKGluZGV4IDwgaW5wdXQubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICAgICAgbGluZS5hcHBlbmQoaW5wdXRbaW5kZXhdKTsgLy8gQXBwZW5kIHRoZSBzdHJpbmcgaW4gdGhlIGNlbGwKCiAgICAgICAgICAgICAgICAgICAgLy8gQ2FsY3VsYXRlIGFuZCBhZGQgdGhlIHBhZGRpbmcKICAgICAgICAgICAgICAgICAgICBpZiAoaiA8IG51bUNvbHMgLSAxKSB7IC8vIE5vIHBhZGRpbmcgbmVlZGVkIGFmdGVyIHRoZSBsYXN0IGNvbHVtbgogICAgICAgICAgICAgICAgICAgICAgICBpbnQgc3BhY2VzID0gbWF4Q29sV2lkdGhzW2pdIC0gaW5wdXRbaW5kZXhdLmxlbmd0aCgpICsgNDsgLy8gNCBzcGFjZXMgYmV0d2VlbiBjb2x1bW5zCiAgICAgICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgc3BhY2VzOyBrKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxpbmUuYXBwZW5kKCIgIik7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGxpbmUudG9TdHJpbmcoKS50cmltKCkpOyAvLyBQcmludCB0aGUgbGluZQogICAgICAgIH0KCgoKCX0KfQ==