PAT乙级练习题B1055. 集体照

来源:互联网 发布:c 高级编程第9版 pdf 编辑:程序博客网 时间:2024/05/27 20:20

题目描述

拍集体照时队形很重要,这里对给定的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

题目解析

排序然后按要求输出;
这里我没有另建一个二维数组,而是直接输出,因为我发现在一排中不论奇偶,只要先左后右排序就没问题;

代码

#include<iostream>#include<algorithm>#include<vector>#include<string>using namespace std;struct person{  int height;  string name;};bool my_compare(person a, person b){  if (a.height != b.height)  {    return a.height > b.height;  }  else  {    return a.name < b.name;  }}int main(){  int N, K,m,m_last;  person per;  vector<person> list_p;  cin >> N >> K;  for (int i = 0; i < N; ++i)  {    cin >> per.name >> per.height;    list_p.push_back(per);  }  sort(list_p.begin(), list_p.end(), my_compare);  m = N / K;  m_last = m + N%m;  for (int i = 0,cnt=0,m_real; i < K; ++i)  {    if (i == 0)    {      m_real = m_last;    }    else    {      m_real = m;    }    string list_i;    for (int j = 0; j < m_real; ++j)    {      if (j == 0)      {        list_i = list_p[cnt++].name;        continue;      }      if (j % 2 == 0)      {        list_i = list_i + " " + list_p[cnt++].name;      }      else      {        list_i = list_p[cnt++].name + " " + list_i;      }    }    cout << list_i << endl;  }  system("pause");  return 0;}
0 0
原创粉丝点击