【发布时间】:2019-08-09 13:32:40
【问题描述】:
我的代码如下。我想计算输出中的字母
s = 字符串,n 是重复的次数。 n = 10,表示字符串 s = "aba" 在 10 个字母的过程中重复。即abaabaabaa。
s, n = input().strip(), int(input().strip())
print(s.count("a")
输出 = 7
我的代码在下面
a = 'aba'
t = list(a)
n = 3
new_list = []
if n <= len(t):
for i in range(n):
new_list.append(t[i])
m = t + new_list
print (''.join(m))
elif n > len(t):
x,y = divmod(n,len(t))
if y == 0:
new_list.append(a * x)
else:
new_list.append((a * x) + ''.join(map(str,t[:y])))
如果 n 很大,则需要像 len(list(s)) = 3 一样循环,如果 n = 10,除以 10/3 得到 3 等分,得到 1 个余数
【问题讨论】: