PAT乙级1055 集体照 (25 分)(思路极简代码)

https://pintia.cn/problem-sets/994805260223102976/problems/994805272021680128

跟螺旋矩阵差不多 稍微简单点
比如题目例子中 我们按身高排序后的序列就是
4 2 1 3
6 5 7
9 8 10
输出分三步:
①输出中间最高者左边的 以2间隔跳步
②输出中间最高
③同①
主要麻烦点在于确定每次for循环的上下界

#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	int h;
	string name;
}; 
node stu[10010];
bool cmp(node a, node b)	{return (a.h == b.h) ? a.name < b.name : a.h > b.h;}
int main(){
	int n, k, max, m, index = 0, centre = 1;
	cin >> n >> k;
	m = n / k;
	max = m + n - m*k;
	for(int i = 1; i <= n; i++)
		cin >> stu[i].name >> stu[i].h;
	sort(stu+1, stu+n+1, cmp);
	for(int i = 0; i < k; i++){
		centre = index + 1;
		int M = (!i) ? max : m;
		int a = (M%2) ? 2 : 1;
		for(int j = (M+centre-a); j > centre; j -= 2)
			cout << stu[j].name << " ";
		cout << stu[centre].name;
		for(int j = centre+2; j <= M+index; j += 2)
			cout << " " << stu[j].name;
		index += M;
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值