PTA 乙级 1055 集体照

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

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

int cmp_height(const void *a, const void *b)
{
	struction *aa = (struction*)a, *bb = (struction*)b;
	return (aa->height > bb->height) ? -1 : 1;
}

int cmp_name(const void *a, const void *b)
{
	struction *aa = (struction*)a, *bb = (struction*)b;
	return strcmp(aa->name, bb->name);
}

int main()
{
	int N, K, n, count = 0;
	struction **b, *a;

	scanf("%d %d", &N, &K);
	n = N / K;
	a = (struction*)malloc(sizeof(struction)*N);
	for (int i = 0; i < N; i++)scanf("%s %d", a[i].name, &a[i].height);
	qsort(a, N, sizeof(struction), cmp_height);
	b = (struction**)malloc(sizeof(struction*)*K);
	b[0] = (struction*)malloc(sizeof(struction)*(N - K * n + n));;
	for (int i = 1; i < K; i++)b[i] = (struction*)malloc(sizeof(struction)*n);
	for (int i = 0; i < N - 1; i++)
		if (a[i].height == a[i + 1].height)
		{
			int cnt = 0, j;
			for (j = i; a[j].height == a[i].height; j++)cnt++;
			qsort(a + i, cnt, sizeof(struction), cmp_name);
			i = j - 1;
		}
	for (int i = (N - K * n + n) / 2, cnt = 0, flag = 1; i >= 0 && i < N - K * n + n; i += cnt * flag)
		b[0][i] = a[count++], cnt++, flag *= -1;
	for (int i = 1; i < K; i++)
		for (int j = n / 2, cnt = 0, flag = 1; j >= 0 && j < n; j += cnt * flag)
			b[i][j] = a[count++], cnt++, flag *= -1;
	for (int i = 0; i < N - K * n + n; i++)
	{
		printf("%s", b[0][i].name);
		if (i != (N - K * n + n - 1))printf(" ");
		else printf("\n");
	}
	for(int i=1;i<K;i++)
		for (int j = 0; j < n; j++)
		{
			printf("%s", b[i][j].name);
			if (j != n - 1)printf(" ");
			else printf("\n");
		}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值