日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

PAT_B_1055_Java(25分)

發(fā)布時間:2023/12/15 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。