感觉是个排序应用。挺像HASH里面的解决冲突的。
整个身高排序,然后按要求排就好啦。
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#define MAX 12
using namespace std;
struct people {
string name;
int high;
};
vector <people> list;
int n,k;
bool cmp(people p1, people p2) {
if (p1.high > p2.high)
return true;
else if (p1.high == p2.high && p1.name < p2.name)
return true;
return false;
}
int main() {
cin >> n >> k;
int m = n / k;
int left = n % k;
int row = n / m;
people formation[10005];
people temp;
for (int i = 0; i < n; i++) {
cin >> temp.name >> temp.high;
list.push_back(temp);
}
sort(list.begin(), list.end(), cmp);
int pos = 0;
for (int i = 0; i < row; i++) {
int tempM = m;
if (i == 0)
tempM = m + left;
int mid = tempM / 2 + 1;
int len = 1;
formation[mid] = list[pos++];
for (int i = 1; i < tempM; i++) {
if (i % 2 != 0) {
formation[mid - len] = list[pos++];
}
else
formation[mid + len++] = list[pos++];
}
for (int i = 1; i < tempM; i++) {
cout << formation[i].name << " ";
}
cout << formation[tempM].name << endl;
}
return 0;
}
身高排序与特殊布局算法
本文介绍了一种基于身高排序的特殊布局算法实现。通过使用C++编程语言,该算法首先收集人员姓名和身高的信息,并按身高降序排列;若身高相同,则按姓名升序排列。接着,算法将人员按照特定布局方式排列,适用于需要特殊布局的场景。
428

被折叠的 条评论
为什么被折叠?



