一、题目
二、示例
三、代码
# 代码 1
while True:
try:
temp = 0
list1 = []
result = []
s = input()
for i in range(0, len(s)):
if s[i].isdigit():
temp += 1
list1.append(s[i])
if i == len(s)-1:
result.append([''.join(list1), temp])
else:
result.append([''.join(list1), temp])
temp = 0
list1 = []
maxL = max(result, key=lambda d : d[:][1])
res = ''
for item in result:
if int(item[1]) == int(maxL[1]):
res += item[0]
print(res + ',' + str(maxL[1]))
except:
break
# 代码 2
while True:
try:
s = input()
for i in s:
if not i.isdigit():
s = s.replace(i, ' ')
s = s.split()
maxL = max(s, key = lambda d : len(d))
res = ''
for i in s:
if len(i) == len(maxL):
res += i
print(res+','+ str(len(maxL)))
except:
break
四、算法说明
-
代码 1 :
逐个遍历字符串中的元素,如果是数字则暂存后继续循环,否则将暂存的字符进行拼接,求出最大长度,再次遍历,把符合最大长度的字符串拼接,打印输出。 -
代码 2 :
把非数字的字符全部替换成空格,然后切割;求出最大长度;再次遍历,把符合最大长度的字符串拼接,打印输出。
胡萝卜
2022年3月7日15:59:41
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |