import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
solution(System.in, new MyPrinter());
}
public static void solution(InputStream in, Printer p) {
Scanner scanner = new Scanner(in);
while (scanner.hasNextLine()) {
int count = scanner.nextInt();
scanner.nextLine();
ArrayList<String> files = new ArrayList<>();
int maxLength = 0;
for (int i = 0; i < count; i ++) {
String line = scanner.nextLine();
files.add(line);
if (line.length() > maxLength) {
maxLength = line.length();
}
}
Collections.sort(files);
int col = 1, totalWidth = maxLength;
while (totalWidth <= 60) {
col ++;
totalWidth += 2 + maxLength;
}
col --;
int fileCount = files.size(), row = fileCount / col;
if (fileCount % col != 0) {
row ++;
}
p.pn(getDashes(60));
for (int i = 0; i < row; i ++) {
for (int j = 0; j < col && j * row + i < fileCount; j ++) {
String f = files.get(j * row + i);
p.p(f);
if ((j + 1) * row + i >= fileCount) {
p.pn(getSpace(maxLength - f.length()));
} else {
p.p(getSpace(maxLength - f.length() + 2));
}
}
}
}
}
private static String getSpace(int count) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i ++) {
builder.append(" ");
}
return builder.toString();
}
private static String getDashes(int count) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i ++) {
builder.append("-");
}
return builder.toString();
}
}
interface Printer {
void p(Object... o);
void pn(Object... o);
default String format(Object... o) {
String format;
if (o.length == 1) {
format = o[0].toString();
} else {
Object[] dest = new Object[o.length - 1];
System.arraycopy(o, 1, dest, 0, dest.length);
format = String.format(o[0].toString(), dest);
}
return format;
}
}
class MyPrinter implements Printer {
@Override
public void p(Object... o) {
System.out.print(format(o));
}
@Override
public void pn(Object... o) {
if ("true".equals(System.getenv("OUTPUT_TO_LOCAL"))){
try {
if (o.length != 0) {
writer.write(format(o));
}
writer.write("\n");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
if (o == null || o.length == 0) {
System.out.println();
} else {
System.out.println(format(o));
}
}
private static FileWriter writer;
static {
try {
writer = new FileWriter("D:\\output.txt");
writer.write("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
uva400 java
最新推荐文章于 2024-04-28 02:11:28 发布