题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3?tpId=37&tqId=21268&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
import sys n = int(input()) while True: ''' 循环接收输入 ''' try: s = input() def count(s): ''' 计算漂亮度函数 ''' #定义字典,存放字符,与该字符对应的出现次数 dict1={} #添加字符x与x对应的个数 for x in s: dict1[x]=s.count(x) #对字典中的个数排序由高到低排序 L = list(sorted(dict1.items(),key=lambda x: x[1],reverse=True)) L3=[] L2 =[] sum=0 for x in L: L2.append(x[1]) #定义一个列表L3,存放分数 for i in range(27): L3.append(i) #将L3按从26-1的顺序排列 L3=L3[::-1] #打包,将排序后的次数和分数打包成元组 c=zip(L2,L3) #遍历元组,按乘积输出求和 for x in c: sum=sum+x[0]*x[1] print(sum) count(s) except: break