题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805272021680128
这题有点麻烦的
核心代码是第37和41行,涉及到3个变量,每隔两个赋值

代码

  1. #include<cstdio>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn = 10010;
  8. struct student{
  9. string name;
  10. int height;
  11. };
  12. vector<student> stu_vec;
  13. bool cmp(student a, student b){
  14. if(a.height!=b.height) return a.height>b.height;
  15. else return a.name<b.name;
  16. }
  17. int main(){
  18. int n, k, m;
  19. scanf("%d %d", &n, &k);
  20. stu_vec.resize(n);
  21. for(int i = 0; i < n ; i++){
  22. cin>>stu_vec[i].name>>stu_vec[i].height;
  23. }
  24. sort(stu_vec.begin(), stu_vec.end(), cmp);
  25. //最后一排
  26. int row = k, t = 0;
  27. while(row > 0){
  28. if(row == k){
  29. m = n - (n / k) * (k - 1);
  30. } else m = n / k ;
  31. vector<string> ans(m);
  32. int mid = m / 2;
  33. ans[mid] = stu_vec[t].name;
  34. int j = mid + 1;
  35. for(int i = t + 2; i < t + m; i += 2){
  36. ans[j++] = stu_vec[i].name;
  37. }
  38. j = mid - 1;
  39. for(int i = t + 1; i < t + m; i += 2){
  40. ans[j--] = stu_vec[i].name;
  41. }
  42. cout << ans[0];
  43. for(int i = 1; i < ans.size(); i++) cout<<" " + ans[i];
  44. cout << endl;
  45. t += m;
  46. row--;
  47. }
  48. return 0;
  49. }