第一题:薪资排名
import sys
import re
import operator
"""
input
张三一:100 李四:984 王五六:835
"""
def main():
line = sys.stdin.readline().strip().split() # 读出这一行的字符串
n = len(line)
score = [0] * n
name = [0] * n
for i in range(0, n): # 将分数与名字分离
totalCount = line[i]
score[i] = int(re.sub("\D", "", totalCount))
name[i] = re.sub("\d", "", totalCount)
print(score)
print(name)
A = dict(zip(name, score)) # 将二者作为键值对写入字典
print(A)
A = sorted(A.items(), key=operator.itemgetter(1), reverse=True) # 字典按值的大小排序(逆序)
ans = str(A[0][0]) + str((A[0][1])) # 按标准字符产格式输出
for i in range(1, n):
ans += ' ' + str(A[i][0]) + str(A[i][1])
print(ans)
if __name__ == "__main__":
main()
第二题 汉诺塔
class Solution:
def Hanoi(self, n):
H = 2 ** n - 1
return H
def main():
s = Solution()
n = int(input())
print(s.Hanoi(n))
if __name__ == "__main__":
main()
第三题 一个字符串中有多少汉字、字母、数字、符号,并输出helloworld
import sys
import re
"""
input:
he123llo,wor84ld!!%[545]你好234。。。
"""
def main():
#line = sys.stdin.readline().strip()
line = 'he123llo,wor84ld!!%[545]你好234。。。'
n = len(line)
num = re.findall("\d", line)
alphabet = re.findall(r'[a-zA-Z]', line)
hanzi = re.findall(r'[\u4e00-\u9fa5]', line)
num_n = len(num)
alphabet_n = len(alphabet)
hanzi_n = len(hanzi)
other_n = n-num_n-hanzi_n-alphabet_n
alphabet_str = ''
for i in range(0,len(alphabet)):
alphabet_str += ''.join(alphabet[i])
b = "{}:计算机{}的cpu占用率为{}%".format("2018-10-10", "C", "10");
ans = "汉字个数:{}, 字母个数:{}, 数字个数:{}, 其它字符个数:{} 输出:{}".format(hanzi_n,alphabet_n,num_n,other_n,alphabet_str)
print(ans)
if __name__ == "__main__":
main()