PAT(A)1109 Group Photo (25point(s))

在这里插入图片描述

Sample Input

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

Sample Output

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

思路:
排序求解。
代码

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct student {
	string name;
	int height;
}stu[10005];
bool cmp(student a, student b) {
	if (a.height != b.height) {
		return a.height>b.height;
	}
	else {
		return a.name<b.name;
	}
}
int main()
{
	int n, k;
	cin >> n >> k;
	int i;
	int m;
	for (i = 0;i<n;i++) {
		cin >> stu[i].name >> stu[i].height;
	}
	sort(stu, stu + n, cmp);
	int t = 0;
	int row = k;
	while (row) {
		if (row == k) {
			m = n - n / k*(k - 1);
		}
		else {
			m = n / k;
		}
		vector<string>stemp(m);
		stemp[m / 2] = stu[t].name;
		int j = m / 2 - 1;
		for (i = t + 1;i<t + m;i += 2) {
			stemp[j--] = stu[i].name;
		}
		j = m / 2 + 1;
		for (i = t + 2;i<t + m;i += 2) {
			stemp[j++] = stu[i].name;
		}
		cout << stemp[0];
		for (i = 1;i<m;i++) {
			cout << " " << stemp[i];
		}
		cout << endl;
		t += m;
		row--;
	}
	return 0;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7087

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104142434