菜鸡的python题解

朕与将军解战袍 发表于 3年前 · 关联问题 性质判断

先说一句:python真香!

# 创建一个列表来记录每个字母对应的次数 a = [] for i in range(26): a.append(0) string = input() str_len = len(string) - 1 i = 0 while(i < str_len): if(string[i] == string[i+1]): # 进行ASCII码的运算,得到字母在列表中对应的下标(num) num = ord(string[i]) - 97 a[num] += 1 i += 1 j = 0 while(j <= 25): pos = a.index(max(a)) # 返回最大值的下标 if(a[pos] != 0): a[pos] = 0 # 将当前的最大值清零,以便寻找下一个最大值 pos += 97 print(chr(pos)+chr(pos),end=' ') else: break j += 1