PAT乙级 1055 集体照 (25 分) Java 实现

首先这里是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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值