多级排序编程题

某公司要针对员工情况统一调薪,现有一个员工列表,包含姓名、职级、工作年限、工资信息。现要求将该列表按以下规则重新排序:

1、职级高的排在前面

2、若职级相同,按工资少的排前面

3、若工资相同,工作年限长的排前面

输入描述:

第一行输入一个整数n,表示员工列表中员工的人数

接下来的n行,分别输入员工的姓名、职级、工作年限和工资信息,各项信息用空格分隔

输出描述:

输出按要求重新排列的员工列表,每名员工信息(姓名、职级、工作年限和工资)占一行

示例

输入:

6
张三 3 3 3000
李四 3 4 3000
王五 3 3 4000
赵六 4 3 3000
陆奇 4 4 4000
闫八 4 4 3980.99 

输出: 

赵六 4 3 3000.00
闫八 4 4 3980.99
陆奇 4 4 4000.00
李四 3 4 3000.00
张三 3 3 3000.00
王五 3 3 4000.00 

方法一:

n = int(input())
emps = [[emp[0],int(emp[1]),int(emp[2]),float(emp[3])] for emp in
        (input().strip().split(" ") for i in range(n))]
   
emps=sorted(emps,key=lambda x:(x[1],-x[3],x[2]), reverse=True)

for emp in emps:print("{} {} {} {:.2f}".format(*emp))

 方法二:

import sys

lines=sys.stdin.readlines()
n=int(lines[0].strip())
emps=[]
for i in range(n):
    tmp=lines[i+1].strip().split(" ")
    tmp[0]=tmp[0].strip()
    tmp[1]=int(tmp[1])
    tmp[2]=int(tmp[2])
    tmp[3]=-float(tmp[3])
    emps.append(tmp)

emps=sorted(emps,key=lambda x:(x[1],x[3],x[2]), reverse=True)

for emp in emps:
    emp[-1]=-emp[-1]
    print("{} {} {} {:.2f}".format(*emp))

方法三:

n = int(input())
emps = [[emp[0],int(emp[1]),int(emp[2]),float(emp[3])] for emp in
        (input().strip().split(" ") for i in range(n))]
   
#基数排序
result = sorted(emps, key=lambda x: -x[2])
result = sorted(result, key=lambda x: x[3])
emps = sorted(result, key=lambda x: -x[1])

for emp in emps:print("{} {} {} {:.2f}".format(*emp))

方法四:

n = int(input())
emps = [[emp[0],int(emp[1]),int(emp[2]),float(emp[3])] for emp in
        (input().strip().split(" ") for i in range(n))]
   
for i in range(n):
    for j in range(n-i-1):
        if emps[j+1][1]<emps[j][1]:
            continue
        elif emps[j+1][1]>emps[j][1]:
            emps[j], emps[j+1] = emps[j+1], emps[j]
        else:
            if float(emps[j+1][3])>float(emps[j][3]):
                continue
            elif float(emps[j+1][3])<float(emps[j][3]):
                emps[j], emps[j+1] = emps[j+1], emps[j]
            else:
                if int(emps[j+1][2])>int(emps[j][2]):
                    emps[j], emps[j+1] = emps[j+1], emps[j]
                else:
                    continue

for emp in emps:print("{} {} {} {:.2f}".format(*emp))

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠溪居士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值