其他分享
首页 > 其他分享> > PAT (Advanced Level) Practice 1109 Group Photo (25 分) 凌宸1642

PAT (Advanced Level) Practice 1109 Group Photo (25 分) 凌宸1642

作者:互联网

PAT (Advanced Level) Practice 1109 Group Photo (25 分) 凌宸1642

题目描述:

Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:

Now given the information of a group of people, you are supposed to write a program to output their formation.

译:合影时,阵型很重要。 给定 N 人形成 K 行的规则如下:

现在给定一组人的信息,你应该编写一个程序来输出他们的阵型。


Input Specification (输入说明):

Each input file contains one test case. For each test case, the first line contains two positive integers N (≤ 104 ), the total number of people, and K (≤10), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).

译:每个输入文件包含一个测试用例。 对于每个测试用例,第一行包含两个正整数 N(≤ 104 ),总人数和 K(≤10),总行数。 然后是N行,每行给出一个人的名字(不超过8个英文字母,不含空格)和他/她的身高([30, 300]中的整数)。


output Specification (输出说明):

For each case, print the formation -- that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.

译:对于每种情况,打印队形——即在 K 行中打印人名。 名称必须正好由一个空格分隔,但每行末尾不得有多余的空格。 注意:因为你是面对人群,所以后排的人必须打印在前排的人上方。


Sample Input (样例输入):

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

Sample Output (样例输出):

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

The Idea:

The Codes:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 10010 ;
struct Peo{
	string name ;
	int height ;
}peo[maxn] ;
int n , k , col , last  ; 
vector<vector<string> > ans ;
bool cmp(Peo a , Peo b){
	if(a.height != b.height) return a.height < b.height ;
	return a.name > b.name ;
}
int main(){
	cin >> n >> k ;
	for(int i = 0 ; i < n ; i ++) cin >> peo[i].name >> peo[i].height ;
	sort(peo , peo + n , cmp) ;
	col = n / k , last = col + n % k ;  // n / k 自动向下取整得到每行人数,最后一行在此基础上加上 n 对 k 的取余的余数
	vector<string> temp(last) ;
	int mid = last / 2 , i = n - 1 ;
	temp[mid] = peo[i --].name ;  // 最后一排最高人位置
	for(int j = 1 ; j < last ; j ++ , i -- ){
		if(j % 2) temp[mid - j/2 - 1] = peo[i].name ;
		else temp[mid + j/2] = peo[i].name ;	
	}
	ans.push_back(temp) ;
	mid = col / 2 ;
	for(int j = 1 ; j < k ; j ++ ){
		vector<string> te(col) ;
		te[mid] = peo[i --].name ;  // 剩下的每行最高人的位置
		for(int s = 1 ; s < col ; s ++ , i -- ){
			if(s % 2) te[mid - s/2 - 1] = peo[i].name ;
			else te[mid + s/2] = peo[i].name ;	
		}
		ans.push_back(te) ;    
	}
	for(int i = 0 ; i < k ; i ++){
		for(int j = 0 ; j < ans[i].size() ; j ++){
			cout << ans[i][j] << ((j == ans[i].size() - 1)?'\n':' ') ;
		}
	}
	return 0 ;
}

标签:25,Group,name,people,int,1642,height,peo,must
来源: https://www.cnblogs.com/lingchen1642/p/15141892.html