首先这里是15/25分的第一次解法。
import java.io.*;
import java.util.Arrays;
public class pat10552 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String[] set = input.readLine().trim().split(" ");
int N = Integer.parseInt(set[0]);
int K = Integer.parseInt(set[1]);
int n = N/K; //每排的人数
int lastN = N%K + K; //最后一排的人数;
People[] people = new People[N];
for(int i=0;i<N;i++) {
String[] arr = input.readLine().trim().split(" ");
people[i] = new People(arr[0], Integer.parseInt(arr[1]));
}
Arrays.sort(people); //排序
//这行是测试排序的 ↓↓↓↓
//for(int i=0;i<N;i++) System.out.println(people[i].name + " " + people[i].high);
for(int i=K;i>0;i--) { //有K行,所以循环K次
int length = n; //用来记录这行要站几个人,初始是每排 N/K=n
if(i==K) length = lastN; //如果是最后一排,就是lastN人
String[] row = new String[length]; //创建一个数组来记录
int arrFrom = i*K-K; //记录从数组的第几个开始输出
int arrTo = arrFrom + length; //输出到第几个
int left = 0, right = row.length-1; //输出左边和右边的位置
boolean isleft = true; //先输出左边
if(i!=K) isleft = false; //这个判断,我也不是很懂,在我写完代码,输出样例以后,发现 按照我的写法先左后右, 只有最后一排是这样的,其它排是先右后左,所以只能加这个条件区分了
for(int j=arrFrom;j<arrTo;j++) {
if(isleft) {
row[left] = people[j].name;
left++;
}
else {
row[right] = people[j].name;
right--;
}
isleft = !isleft; //反转 isleft
}
for(int j=0;j<row.length-1;j++) System.out.print(row[j] + " ");
System.out.println(row[row.length-1]);
}
}
}
class People implements Comparable<People> {
String name;
int high;
public People(String name, int high) {
this.name = name;
this.high = high;
}
@Override
public int compareTo(People peo) {
if (this.high < peo.high) return -1;
else if (this.high > peo.high) return 1;
else return -this.name.compareTo(peo.name);
}
}
由于这个方法有超时,所以不得不祭出Tokenizer。
数据输入的片段改成:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(input);
st.nextToken();
int N = (int)st.nval;
st.nextToken();
int K = (int)st.nval;
int n = N/K; //每排的人数
int lastN = N%K + K; //最后一排的人数;
People[] people = new People[N];
for(int i=0;i<N;i++) {
st.nextToken();
String name = st.sval;
st.nextToken();
int high = (int)st.nval;
people[i] = new People(name, high);
}
把超时的问题解决后,就开始改测试点错误了。
import java.io.*;
import java.util.Arrays;
public class pat1055 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(input);
st.nextToken();
int N = (int)st.nval;
st.nextToken();
int K = (int)st.nval;
int n = N/K; //每排的人数
People[] people = new People[N];
for(int i=0;i<N;i++) {
st.nextToken();
String name = st.sval;
st.nextToken();
int high = (int)st.nval;
people[i] = new People(name, high);
}
Arrays.sort(people); //排序
//这行是测试排序的 ↓↓↓↓
//for(int i=0;i<N;i++) System.out.println(people[i].name + " " + people[i].high);
int arrTo = 0; //记录people数组输出到了哪里,即为下一循环的开始
int length; //这一排的人数
int arrFrom; //记录从数组的第几个开始输出
while (arrTo < N){
arrFrom = arrTo;
if(arrTo==0) arrTo += n+(N-K*n); //判断是不是第一行
else arrTo += n;
length = arrTo-arrFrom;
String[] row = new String[length]; //创建一个数组来记录
int mid = length/2;
for (int l = arrFrom; l < arrTo; l++) {
mid += (int)Math.pow(-1,l-arrFrom)*(l-arrFrom);
row[mid] = people[l].name;
}
for(int j=0;j<row.length-1;j++) System.out.print(row[j] + " ");
System.out.println(row[row.length-1]);
}
}
}
class People implements Comparable<People> {
String name;
int high;
public People(String name, int high) {
this.name = name;
this.high = high;
}
@Override
public int compareTo(People peo) {
if (this.high < peo.high) return 1;
else if (this.high > peo.high) return -1;
else return this.name.compareTo(peo.name);
}
}