PAT B1055

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
  char name[10];
  int H;
}Stu[10001];
int cmp(const void *a,const void *b)
{  
  struct student *m=(struct student*)a;
  struct student *n=(struct student*)b;//此时我们定义了两个结构体指针
  if(m->H==n->H)//从小到大排序
    return strcmp(m->name,n->name);
  else
    return n->H-m->H;
}
int main()
{
  int num,row,i,j,k,f,right,left,count=0,re,rs,wigth,pe=1;
  char *tmp[10000];
  scanf("%d%d",&num,&row);
  for(i=0;i<num;i++)
  {
    scanf("%s%d",Stu[i].name,&Stu[i].H);
  }
  qsort(Stu,num,sizeof(Stu[0]),cmp);
  rs=num%row;//rs为多余出来的人数。
  re=num/row;//re 基础人数。
  for(i=0,j=0;i<row;i++,pe=1,rs=0)
  {
    wigth=rs+re;//最后一排就是最多的人数
    right=(wigth/2);//因为我们的下标是从0开始所以我们就不用加一了;
    left=right-1;
    for(k=0;k<wigth;k++)
    {
      if(pe==1) tmp[right++]=Stu[j++].name;
      if(pe==-1) tmp[left--]=Stu[j++].name;
      pe*=-1;//来回切换位置
    }
        for (f= 0; f< wigth; f++) // 顺序输出
    {
      printf("%s", tmp[f]);
      if(f<wigth-1)
        printf(" ");
    }
        printf("\n");
  
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值