PAT_B_1055_Java(25分)
生活随笔
收集整理的這篇文章主要介紹了
PAT_B_1055_Java(25分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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; // 總?cè)藬?shù)private static int totalRows; // 總行數(shù)private static BufferedReader getBufferedReader() {// 獲取輸入流InputStreamReader isr = new InputStreamReader(System.in);return new BufferedReader(isr);}private static void setTotalNumber() throws Exception {// 設(shè)置總?cè)藬?shù)和總排數(shù)String str = input.readLine();String[] arr = str.split(" ");totalPeople = Integer.parseInt(arr[0]); // 總?cè)藬?shù)totalRows = Integer.parseInt(arr[1]); // 總排數(shù)}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]); // 設(shè)置姓名int height = Integer.parseInt(arr[1]);people[i].setHeight(height); // 設(shè)置身高}return people; // 返回所有人}private static void outputFormation(People[] people) {// 輸出隊形int rowNumber = totalPeople / totalRows;// 每排的人數(shù)int from = 0;// 每排的起始下標(閉),默認為最后一排的起始下標// 每排的結(jié)束下標(開),默認為最后一排的結(jié)束下標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; // 下一排的結(jié)束下標}}private static List<People> getRowsPeople( People[] people, // 所有人int from, // 起始下標(閉)int to) {// 結(jié)束下標(開)int length = to - from;// 這一排的人數(shù)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(); // 設(shè)置總?cè)藬?shù)和總排數(shù)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) { // 設(shè)置姓名this.name = name;}public int getHeight() { // 獲取身高return height;}public void setHeight(int height) { // 設(shè)置身高this.height = height;}@Overridepublic int compareTo(People o) {int anotherHeight = o.getHeight(); // 別人的身高if (height != anotherHeight) { // 身高不等return anotherHeight - height; // 身高降序}String anotherName = o.getName(); // 別人的姓名return name.compareTo(anotherName); // 姓名字典序升序}
}
總結(jié)
以上是生活随笔為你收集整理的PAT_B_1055_Java(25分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用组合数计算公式及推算[通俗易懂]
- 下一篇: PAT_B_1057_Java(20分)