1 题目内容
已知有以下列表:
#员工编号,姓名,基本工资,奖金,住房补助,五险一金,其他扣款,专项附加扣除。
incomeList = [
['10932','张珊',15000.00 ,4000.00 ,2300.00 ,980.00 ,300.00 ,1000.00],
['10933','李思',12000.00 ,5000.00 ,2600.00 ,890.00 ,400.00 ,2000.00],
['10934','王武',10100.00 ,2000.00 ,2500.00 ,860.00 ,500.00 ,1500.00],
['10935','赵柳',10050.00 ,1000.00 ,2700.00 ,1080.00 ,300.00 ,2000.00],
['10936','钱棋',10200.00 ,2000.00 ,2400.00 ,1000.00 ,600.00 ,1000.00],
['10941','张明',13600.00 ,4000.00 ,2100.00 ,680.00 ,300.00 ,1000.00],
['10942','赵敏',13200.00 ,2500.00 ,2300.00 ,670.00 ,400.00 ,3000.00],
['10945','王红',13600.00 ,2600.00 ,2030.00 ,560.00 ,400.00 ,2000.00],
['10946','李萧',12500.00 ,2800.00 ,2400.00 ,420.00 ,500.00 ,1500.00],
['10947','孙科',12000.00 ,3500.00 ,2030.00 ,880.00 ,300.00 ,1000.00],
['10948','刘利',14200.00 ,2500.00 ,2020.00 ,900.00 ,400.00 ,2000.00]
]
编写程序,根据用户输入的表达式进行列表降序排序后,输出员工编号,编号之间用逗号分隔。列入用户输入2+3,或2+3-7,代表需要按照第二项与第三项的和进行升序排序,或第2,3项的和与第7项的差进行升序排序。(注:表达式中仅允许出现:2,3,4,5,6,7,+,-这几个字符。)
输入格式:
表达式
输出格式:
逗号分隔的员工编号
输入样例:
2+3
输出样例:
10935, 10934, 10936, 10946, 10947, 10942,10945, 10948, 10933, 10941, 10932
2 思路分析
由题得知这道题是个典型的排序问题,难点在于表达式的处理与员工号与处理数据的排序并输出。
表达式的处理,因为表达式一定式‘+,-’与数字交替出现的,所以可以单得定义一个函数来处理。
排序就将编号和处理好的数据放在一起,用冒泡排序排好输出就行了。
3 python代码
incomeList = [
['10932','张珊',15000.00 ,4000.00 ,2300.00 ,980.00 ,300.00 ,1000.00],
['10933','李思',12000.00 ,5000.00 ,2600.00 ,890.00 ,400.00 ,2000.00],
['10934','王武',10100.00 ,2000.00 ,2500.00 ,860.00 ,500.00 ,1500.00],
['10935','赵柳',10050.00 ,1000.00 ,2700.00 ,1080.00 ,300.00 ,2000.00],
['10936','钱棋',10200.00 ,2000.00 ,2400.00 ,1000.00 ,600.00 ,1000.00],
['10941','张明',13600.00 ,4000.00 ,2100.00 ,680.00 ,300.00 ,1000.00],
['10942','赵敏',13200.00 ,2500.00 ,2300.00 ,670.00 ,400.00 ,3000.00],
['10945','王红',13600.00 ,2600.00 ,2030.00 ,560.00 ,400.00 ,2000.00],
['10946','李萧',12500.00 ,2800.00 ,2400.00 ,420.00 ,500.00 ,1500.00],
['10947','孙科',12000.00 ,3500.00 ,2030.00 ,880.00 ,300.00 ,1000.00],
['10948','刘利',14200.00 ,2500.00 ,2020.00 ,900.00 ,400.00 ,2000.00]
]
s=input()
ass=[]
def mysum(s,box):
if s[0] !='-':
sum=box[int(s[0])]
for i in range(1,len(s)):
if i%2==0:
if s[i-1]=='+':
sum+=box[int(s[i])]
if s[i-1]=='-':
sum-=box[int(s[i])]
return sum
else:
sum=0
for i in range(1,len(s)):
if i%2!=0:
if s[i-1]=='+':
sum+=box[int(s[i])]
if s[i-1]=='-':
sum-=box[int(s[i])]
return sum
for i in range(len(incomeList)):
ass.append([incomeList[i][0],mysum(s,incomeList[i])])
for j in range(len(ass)-1):
for k in range(len(ass)-1):
tmp=ass[k]
if ass[k][1]>ass[k+1][1]:
ass[k]=ass[k+1]
ass[k+1]=tmp
for i in range(len(ass)):
ass[i]=ass[i][0]
print(','.join(ass))
4 总结
冒泡排序是最简单的排序算法,可以用来解决sort函数不能解决的问题。虽然简单,但如果能熟练运用,对以后的编程思路有很大的提升。
END
编 辑 | 王楠岚
责 编 | 王自强
where2go 团队