https://pintia.cn/problem-sets/994805046380707840/problems/994805091888906240
解析
看代码注释即可…
AC Code
public static void main(String[] args) throws Exception {
int n = nextInt(); // row
char[] A = br.readLine().trim().toCharArray();
int m = 0; // col
for(int i = 0; ; i++) {
m = i;
if(i * n >= A.length) break;
}
ArrayList<Character>[] data = new ArrayList[m];
int cnt = 0;
for(int i = m - 1; i >= 0; i--) { // 从右到左
data[i] = new ArrayList<>();
for(int j = 0; j < n && cnt < A.length; j++) { // 依次添加字符
data[i].add(A[cnt++]);
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) { // 逐行遍历
for(int j = 0; j < m; j++) { // 该行某列
if(i < data[j].size() && data[j].get(i) != null) sb.append(data[j].get(i));
else sb.append(" ");
}
sb.append("\n");
}
out.print(sb);
out.flush();
}