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