PAT甲1109 Group Photo(25 分)

#include <string.h>
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <string>
#include <iostream>
#include <math.h>
using namespace std;

struct student
{
    int height;
    char name[20];
}stu[10010];

int N,K;

int G[20][10010];

bool cmp(student a,student b)
{
    if(a.height!=b.height)return a.height>b.height;
    else return strcmp(a.name,b.name)<0;
}

int num=0;
void fillg(int m,int row)
{
    int start=m/2;
    int x=start-1;
    int y=start+1;
    G[row][start]=num++;
    int step=1;
    while(step<m)
    {
        if(x>=0&&step<m)
        {
            G[row][x]=num++;
            step++;
            x--;
        }
        if(y<m&&step<m)
        {
            G[row][y]=num++;
            step++;
            y++;
        }
    }
}

int main()
{
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++)
    {
        scanf("%s %d",&stu[i].name,&stu[i].height);
    }
    sort(stu,stu+N,cmp);
    int average=N/K;
    int last=N-average*(K-1);
    fillg(last,0);
    for(int i=1;i<K;i++)
    {
        fillg(average,i);
    }
    for(int i=0;i<last;i++)
    {
        int index=G[0][i];
        if(i!=0)printf(" ");
        printf("%s",stu[index].name);
    }
    printf("\n");
    for(int i=1;i<K;i++)
    {
        for(int j=0;j<average;j++)
        {
            int index=G[i][j];
            if(j!=0)printf(" ");
            printf("%s",stu[index].name);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值