1109. Group Photo (25)

13 篇文章 7 订阅
  1. 题目见这里

  2. 实际上是结构体的排序,然后按特定规则重新填充

  3. 代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10005

typedef struct{
    int height;
    char name[9];
}People;

People people[N],tmp[N];
int n,k;

void Read(){
    int i;
    scanf("%d%d",&n,&k);
    getchar();
    for(i=0;i<n;i++){
        scanf("%s%d",people[i].name,&people[i].height);
        getchar();
    }
}

void Copy(People *to, People *from){
    int i;
    for(i=0;i<n;i++){
//      to[i].height = from[i].height;
//      strcpy(to[i].name,from[i].name);
        to[i] = from[i];
    }
}

//1.身高从高到底
//2.身高相同,按姓名字母表顺序排(小在前) 
int Cmp(const void *a, const void *b){
    People pa = *(People *)a;
    People pb = *(People *)b;
    if(pa.height!=pb.height) return pb.height-pa.height;
    else return strcmp(pa.name,pb.name);
}

void Solve(){
    int s = 0;
    int j = -1;
    int i,rowP,step,front,rear;
    for(i=0;i<k;i++){
        if(i==0) rowP = n/k+n%k;
        else rowP = n/k;
        people[rowP/2+s] = tmp[++j];
        rear = front = rowP/2+s;
        step = 1;
        while(step<rowP){
            if(step%2) people[--front] = tmp[++j];
            else people[++rear] = tmp[++j];
            step ++;
        }
        s += rowP;
    }
}

void Show(){
    int s = 0;
    int i,j,rowP;
    for(i=0;i<k;i++){
        if(!i) rowP = n/k+n%k;
        else rowP = n/k;
        for(j=s;j<s+rowP;j++){ 
            printf("%s",people[j].name);    
            if(j<s+rowP-1) printf(" "); 
            else printf("\n");
        }
        s += rowP;
    }
}

int main(){
//  freopen("Data.txt","r",stdin);
    Read();
    Copy(tmp,people);
    qsort(tmp,n,sizeof(tmp[0]),Cmp);
    Solve();
    Show();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值