PAT (Basic Level) Practise (中文) 1055. 集体照 (25)

1055. 集体照 (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下:

  • 每排人数为N/K(向下取整),多出来的人全部站在最后一排;
  • 后排所有人的个子都不比前排任何人矮;
  • 每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整);
  • 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);
  • 若多人身高相同,则按名字的字典序升序排列。这里保证无重名。

现给定一组拍照人,请编写程序输出他们的队形。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出两个正整数N(<=10000,总人数)和K(<=10,总排数)。随后N行,每行给出一个人的名字(不包含空格、长度不超过8个英文字母)和身高([30, 300]区间内的整数)。

输出格式:

输出拍照的队形。即K排人名,其间以空格分隔,行末不得有多余空格。注意:假设你面对拍照者,后排的人输出在上方,前排输出在下方。

输入样例:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
输出样例:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
 

     这个是改良版
 * // 测试点3,4,5超时

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        People[] peoples = new People[n];
        for (int i = 0; i < n; i++) {
            peoples[i] = new People(in.next(), in.nextInt());
        }
        in.close();

        Arrays.sort(peoples);

        int per = n / k;
        for (int i = k; i >= 1; i--) {
            int len = 0;
            int index = 0;
            if (i == k) {    
                len = n - k * per + per;
                index = n-1;
            } else {
                len = per;
                index = i * per - 1;
            }
            
            People[] temp = new People[len];
            int left = len / 2;
            int right = len / 2;
            temp[left] = peoples[index--];
            left--;
            right++;
            while (left >= 0 && right < len) {
                temp[left--] = peoples[index--];
                temp[right++] = peoples[index--];
            }
            
            if (left >= 0) {
                temp[left] = peoples[index];
            } else if (right < len) {
                temp[right] = peoples[index];
            }
            
            for (int j = 0; j < len - 1; j++) {
                System.out.print(temp[j].name + " ");
            }
            System.out.println(temp[len - 1].name);
        }
    }

}

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 o) {
        if (this.high < o.high) {
            return -1;
        } else if (this.high > o.high) {
            return 1;
        } else {
            return -this.name.compareTo(o.name);
        }
    }
}


import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class num1055 {
    //这个是把每一排的排出来了,中间大两边小没实现,高度一样按姓名排序也没实现
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int m=input.nextInt();
        Map<String,Integer> map=new HashMap<String,Integer>();
        for (int i = 0; i < n; i++) {
            map.put(input.next(), input.nextInt());
        }
        List<Map.Entry<String, Integer>> infoIds =
                new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {   
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {      
                return (o2.getValue() - o1.getValue());
                //return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });
        for (int i = 0; i < infoIds.size(); i++) {
             String id = infoIds.get(i).toString().split("=")[0];
             System.out.print(id);
            
            if (i%3==0&&i!=0) {
                System.out.println();
            }else {
                System.out.print(" ");
            }
           
        }
    }
}
/*如何在集合中用集合的某个元素排序,这个
 * Map<String, Integer> map = new HashMap<String, Integer>();
map.put("d", 2);
map.put("c", 1);
map.put("b", 1);
map.put("a", 3);

List<Map.Entry<String, Integer>> infoIds =
    new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

//排序前
for (int i = 0; i < infoIds.size(); i++) {
    String id = infoIds.get(i).toString();
    System.out.println(id);
}
//d 2
//c 1
//b 1
//a 3

//排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {   
    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {      
        //return (o2.getValue() - o1.getValue());
        return (o1.getKey()).toString().compareTo(o2.getKey());
    }
});

//排序后
for (int i = 0; i < infoIds.size(); i++) {
    String id = infoIds.get(i).toString();
    System.out.println(id);
}
//根据key排序
//a 3
//b 1
//c 1
//d 2
//根据value排序
//a 3
//d 2
//b 1
//c 1
 *
 *

 */

c++

博客源地址http://blog.csdn.net/qq_30091945/article/details/54882220

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

struct Person{
    string Name;
    int Height;
    bool operator < ( const Person& a)const{
        if (Height != a.Height){
            return Height > a.Height;
        }else{
            return strcmp(Name.c_str(),a.Name.c_str()) < 0;
        }
    }
};

void Print(int index ,int n,struct Person* person)
{
    int I[n];
    I[n/2] = index;
    int left = n / 2 - 1;
    int right = n / 2 + 1;
    int i = index+1;
    while ( left >= 0 || right < n){
        if ( left >= 0){
            I[left--] = i++; 
        }
        if ( right < n){
            I[right++] = i++;
        } 
    }

    cout<<person[I[0]].Name;
    for ( int i = 1 ; i < n ; i++){
        cout<<" "<<person[I[i]].Name;
    }
    cout<<endl;
}

int main()
{
    int Num,Col,Row;
    cin>>Num>>Col;
    Row = Num / Col;
    struct Person person[Num];
    for ( int i = 0 ; i < Num ; i++){
        cin>>person[i].Name>>person[i].Height;
    }
    sort(person,person+Num);
    Print(0,Row + Num % Col,person);
    for ( int i = Row + Num % Col ; i < Num ; i += Row ){
        Print(i, Row , person);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值