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;
}