华为OD社招 C++ 技术一面
5.6 上机考试
5.13 技术一面
一上来就是手撕代码环节
【按身高和体重排队】
某学校举行运动会,学生们按编号(1、2、3…n)进行标识,现需要按照身高由低到高排列,对身高相同的人,按体重由轻到重排列;对于身高体重都相同的人,维持
原有的编号顺序关系。请输出排列后的学生编号。
输入描述:两个序列,每个序列由n个正整数组成(0 < n <= 100)。第一个序列中的数值代表身高,第二个序列中的数值代表体重。
输出描述:排列结果,每个数值都是原始序列中的学生编号,编号从1开始
示例1:
输入
4
100 100 120 130
40 30 60 50
输出
2 1 3 4
某学校举行运动会,学生们按编号(1、2、3…n)进行标识,现需要按照身高由低到高排列,对身高相同的人,按体重由轻到重排列;对于身高体重都相同的人,维持
原有的编号顺序关系。请输出排列后的学生编号。
输入描述:两个序列,每个序列由n个正整数组成(0 < n <= 100)。第一个序列中的数值代表身高,第二个序列中的数值代表体重。
输出描述:排列结果,每个数值都是原始序列中的学生编号,编号从1开始
示例1:
输入
4
100 100 120 130
40 30 60 50
输出
2 1 3 4
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int>height(n);
vector<int>weight(n);
for (int i = 0; i < n; i++)
cin >> height[i];
for (int i = 0; i < n; i++)
cin >> weight[i];
vector<vector<int>>student;
for (int i = 0; i < n; i++) {
vector<int>t(3);
t[0] = i + 1;
t[1] = height[i];
t[2] = weight[i];
student.push_back(t);
}
auto cmp = [](const vector<int>& stu1, const vector<int>& stu2) {
if (stu1[1] < stu2[1])
return true;
else if (stu1[1] == stu2[1])
return stu1[2] < stu2[2];
else
return false;
};
//sort(student.begin(), student.end(), cmp); 不稳定
stable_sort(student.begin(), student.end(), cmp);
for (auto it : student)
cout << it[0] << " ";
return 0;
} 
上海得物信息集团有限公司公司福利 1182人发布