# 生成字母表
alph = []
for i in range(65,91):
alph.append(chr(i))
# 输入处理
s_n = []
s = input()
s_list = s.split(' ')
s = s_list[0]
m = int(s_list[1])
# 编号
n = len(s)
for i in range(n):
for j in range(len(alph)):
if s[i] == alph[j]:
s_n.append(j)
break
# 计算次数
sum_n = 0
i = 0
flag = 0
while i < n-1:
d = abs(s_n[i+1] - s_n[i])
if d == 1:
sum_n += 1
elif d == 25:
sum_n += 0
elif d > 1:
if flag == 0:
flag += 1
sum_n += m
i += m-1
else:
sum_n += d
else:
sum_n += 0
i += 1
sum_n += n
print(sum_n)
华为机考试题:光标移动输入字符串
最新推荐文章于 2024-08-24 21:28:27 发布