import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
private static BufferedReader input; // 输入流
private static int totalPeople; // 总人数
private static int totalRows; // 总行数
private static BufferedReader getBufferedReader() {// 获取输入流
InputStreamReader isr = new InputStreamReader(System.in);
return new BufferedReader(isr);
}
private static void setTotalNumber() throws Exception {// 设置总人数和总排数
String str = input.readLine();
String[] arr = str.split(" ");
totalPeople = Integer.parseInt(arr[0]); // 总人数
totalRows = Integer.parseInt(arr[1]); // 总排数
}
private static People[] getPeople() throws Exception {// 获取所有人的信息
People[] people = new People[totalPeople];// 保存所有人
for (int i = 0; i < totalPeople; ++i) {
people[i] = new People();
String str = input.readLine(); // 读取一行信息
String[] arr = str.split(" ");
people[i].setName(arr[0]); // 设置姓名
int height = Integer.parseInt(arr[1]);
people[i].setHeight(height); // 设置身高
}
return people; // 返回所有人
}
private static void outputFormation(People[] people) {// 输出队形
int rowNumber = totalPeople / totalRows;// 每排的人数
int from = 0;// 每排的起始下标(闭),默认为最后一排的起始下标
// 每排的结束下标(开),默认为最后一排的结束下标
int to = rowNumber + totalPeople % totalRows;
for (int i = 0; i < totalRows; ++i) {// 所有排
List<People> row = getRowsPeople(people, from, to);// 获取一排的人
outputRow(row); // 输出这一排的人的姓名
from = to; // 下一排的起始下标
to += rowNumber; // 下一排的结束下标
}
}
private static List<People> getRowsPeople( People[] people, // 所有人
int from, // 起始下标(闭)
int to) {// 结束下标(开)
int length = to - from;// 这一排的人数
List<People> row = new ArrayList<People>(length);// 初始化长度为length的列表
boolean dir = true;// 插入列表的方向,true表示在末尾插入
for (int i = from; i < to; ++i) {
if (dir) {
row.add(people[i]); // 列表末尾插入
dir = false; // 改变方向为首部插入
} else {
row.add(0, people[i]); // 列表首部插入
dir = true; // 改变方向为末尾插入
}
}
return row;// 返回这一排的人
}
private static void outputRow(List<People> row) {// 输出row这一排的人
int length = row.size();
for (int i = 0; i < length; ++i) {
if (i > 0) {
System.out.print(" ");// 不是第一个人,前面输出空格
}
People people = row.get(i);
String name = people.getName();
System.out.print(name);
}
System.out.println();
}
public static void main(String[] args) throws Exception {
input = getBufferedReader(); // 获取输入流
setTotalNumber(); // 设置总人数和总排数
People[] people = getPeople(); // 获取所有人
Arrays.sort(people); // 排序
outputFormation(people); // 输出队形
}
}
class People implements Comparable<People> {// 人
private String name; // 姓名
private int height; // 身高
public String getName() { // 获取姓名
return name;
}
public void setName(String name) { // 设置姓名
this.name = name;
}
public int getHeight() { // 获取身高
return height;
}
public void setHeight(int height) { // 设置身高
this.height = height;
}
@Override
public int compareTo(People o) {
int anotherHeight = o.getHeight(); // 别人的身高
if (height != anotherHeight) { // 身高不等
return anotherHeight - height; // 身高降序
}
String anotherName = o.getName(); // 别人的姓名
return name.compareTo(anotherName); // 姓名字典序升序
}
}
PAT_B_1055_Java(25分)
猜你喜欢
转载自blog.csdn.net/qq_43511405/article/details/107381228
今日推荐
周排行