题目
思路
原序列按身高从大到小,名字从小到大排序后,分段进行新队列的赋值
这个鬼题的四舍五入我是没看懂
代码
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
const int nmax = 1005;
struct info {
string name;
int high;
};
vector<info> vec;
vector<string> q;
bool cmp(const info& a, const info& b) {
if (a.high != b.high)
return a.high > b.high;
else
return a.name < b.name;
}
void handle(int low, int high, int m) {
int middle = m / 2;
int cnt = 1;
for (int i = low; i <= high; ++i) {
q[low + middle] = vec[i].name;
if (cnt % 2 != 0)
middle -= cnt++;
else
middle += cnt++;
}
}
int main()
{
int n, row;
cin >> n >> row;
info tmp;
q.resize(n);
for (int i = 0; i < n; ++i) {
cin >> tmp.name >> tmp.high;
vec.emplace_back(tmp);
}
sort(vec.begin(), vec.end(), cmp);
int rowpeople = n / row;
int remain = 0;
if (n % row != 0) {
if (n / row != 0) {
remain = n % row + rowpeople;
}
else
remain = n % row;
}
if (remain != 0) {
handle(0, remain - 1, remain);
for (int j = 0; j < remain; ++j) {
j == 0 ? cout << q[j] : cout << " " << q[j];
}
cout << endl;
}
for (int i = remain; i < n; i += rowpeople) {
handle(i, i + rowpeople - 1, rowpeople);
for (int j = i; j < i + rowpeople; ++j) {
j == i ? cout << q[j] : cout << " " << q[j];
}
cout << endl;
}
}