码迷,mamicode.com
首页 > 其他好文 > 详细

1055. 集体照 (25)

时间:2017-03-19 15:24:59      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:思路   输入   运算   pause   实现   数组   style   man   程序   

1055. 集体照 (25)

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

 

 

 

 

思路:

  1.不使用数组而使用string的"+""-"运算符来实现人员的插入

注意: 字典序升序排列时,不能只比较首字母,应比较整个字符串

#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
struct student
{
    string name;
    int high;
}stu[10050];

bool cmp(student a, student b)
{
    if (a.high != b.high)
        return a.high > b.high;
    else
        return a.name < b.name;
}
int main()
{
    int n, k;
    cin >> n >> k;
    int m = n / k;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].name >> stu[i].high;
    }
    sort(stu, stu + n, cmp);
    

    
        int first = n - m*(k - 1);
        int cnt = 0 ;
        string first;//第一排输出,即最后一排
        first = stu[cnt++].name;
        int flag = 1;//
        while (cnt != first)
        {
            if (flag == 1)//左边插入
            {
                first = stu[cnt++].name+ +first;
                flag = -flag;
            }
            else if (flag == -1)//右边插入
            {
                first = first+ +stu[cnt++].name;
                flag = -flag;
            }

        }
        cout << first<<endl;

        for (int i = 0; i < k - 1; i++)
        {
            int cnt_t = 0;
            string temp;
            temp = stu[cnt++].name;
            cnt_t++;
            int flag_t = 1;
            while (cnt_t != m)
            {
                if (flag_t == 1)//左边插入
                {
                    temp = stu[cnt++].name +   + temp;
                    cnt_t++;
                    flag_t = -flag_t;
                }
                else if (flag_t == -1)//右边插入
                {
                    temp = temp +   + stu[cnt++].name;
                    cnt_t++;
                    flag_t = -flag_t;
                }
            }
            cout << temp << endl;
        }





    system("pause");
    return 0;
}

 

1055. 集体照 (25)

标签:思路   输入   运算   pause   实现   数组   style   man   程序   

原文地址:http://www.cnblogs.com/brightz2017/p/6580352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!