OJ【身高体重排序】

题目描述:

某学校举行运动会,学生们按编号(1、2、3…n)进行标识,现需要按照身高由低到高排列,对身高相同的人,按体重由轻到重排列;对于身高体重都相同的人,维持原有的编号顺序关系。请输出排列后的学生编号。

输入描述:

两个序列,每个序列由n个正整数组成(0 < n <= 100)。第一个序列中的数值代表身高,第二个序列中的数值代表体重。

输出描述:

排列结果,每个数值都是原始序列中的学生编号,编号从1开始,身高从低到高,身高相同体重从轻到重,体重相同维持原来顺序。

样例1:

输入:

4

100 100 120 130

40 30 60 50

输出:

2 1 3 4 

C++解答

解答思路: 

将每一位同学的身高、体重和序号放到一个数组中,多位同学组成一个二维数组,然后对二维数组按照下面的优先级进行排序,排序后,最后一列的序号则是答案。

这里用到sort函数排序,排序函数需要自己定义,身高为第一优先级,身高相同判断体重,都相同则根据序号排列。

代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class Solution {
    static bool cmp(const vector<int> &a, const vector<int> &b) {
        return a[0] < b[0] || (a[0] == b[0] && a[1] < b[1]) || (a[0] == b[0] && a[1] == b[1] && a[2] < b[2]);
    }
public:
    vector<int> getHeightSort(const vector<int> &heights, const vector<int> &weights)
    {
        int size = heights.size();
        vector<vector<int>> ans(size, vector<int>(3));
        for(int i = 0; i < size; i++) {
            ans[i][0] = heights[i];
            ans[i][1] = weights[i];
            ans[i][2] = i+1;
        }
        sort(ans.begin(), ans.end(), cmp);
        vector<int> result;
        for(int i = 0; i < size; i++ ) {
            result.push_back(ans[i][2]);
        }
        return result;
    }
};


inline int readInt() 
{
    int size;
    cin >> size;
    return size;
}

template<typename T>
inline vector<T> readVector(int size)
{
    vector<T> obj(size);
    for (int i = 0; i < size; i++) {
        cin >> obj[i];
    }
    return obj;
}

template<typename T>
inline void writeVector(const vector<T> &obj, char delimeter = ' ')
{
    auto it = obj.begin();
    if(it == obj.end()) {
        return ;
    }
    cout << *it;
    for(++it; it != obj.end(); it++ ) {
        cout << delimeter << *it;
    }
    cout << endl;
}
int main()
{

    int count = readInt();
    vector<int> heights = readVector<int>(count);
    vector<int> weights = readVector<int>(count);
    Solution solu;
    vector<int> result = solu.getHeightSort(heights, weights);
    writeVector(result);
    return 0;
}

python解答:

#!/usr/bin/env python
# coding=utf-8
class Solution:
    def getHeightSort(self, heights, weights):
        size = len(heights)
        res = []
        l = [[i+1, heights[i], weights[i]] for i in range(size)]
        ans_l = sorted(l, key = lambda x : (x[1], x[2], x[0]))
        for k in range(size):
            res.append(ans_l[k][0])
        return res
        
if __name__ == "__main__":
    count = int(input().strip())
    heights = list(map(int, input().strip().split()))
    weights = list(map(int, input().strip().split()))
    function = Solution()
    result = function.getHeightSort(heights, weights)
    print(" ".join(map(str, result)))

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值