PTA 古风排版 Java

PTA 古风排版 Java

在这里插入图片描述

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		Writer.init(System.out);
		slove();
		Writer.close();
	}

	// 解答入口
	static void slove() throws IOException {
		int row = Reader.nextInt();
		char[] chs = Reader.nextLine().toCharArray();
		// 如果是 3 / 4 = 0 所以最后的计算结果要 + 1
		// 但是这样做的话, 12 / 4 + 1 = 4 会多出来一列, 所以将整体的长度 -1 
		// 即 ( 12 - 1 ) / 4 + 1 = 3
		int column = (chs.length - 1) / row + 1;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				int index = (column - j - 1) * row + i;
				if(index >= chs.length) {
					Writer.print(" ");
				}else {
					Writer.print(chs[index]);
				}
			}
			Writer.print("\n");
		}
	}

}

// 以下是输入输出模板和思路逻辑无关
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}

	static Double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

class Writer {
	static BufferedWriter writer;

	static void init(OutputStream outputStream) {
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));
	}

	static void print(Object object) throws IOException {
		writer.write(object.toString());
	}

	static void println(Object object) throws IOException {
		writer.write(object.toString());
		writer.write("\n");
	}

	static void close() throws IOException {
		// TODO Auto-generated method stub
		writer.close();
	}
}

发布了27 篇原创文章 · 获赞 2 · 访问量 1469

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104208739