给定两个均不超过9的正整数a和n,要求编写函数fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和
下面函数部分代码
def fn (a, b):
s = 0
n = 0
for i in range(b):
n = n*10 + a
s += n
return s
# 方法二
def fn(a,n):
result=0
for i in range(1,n+1):
result=int(str(a)*i)+result
return result
下面是效果展示
def fn (a, b):
s = 0
n = 0
for i in range(b):
n = n*10 + a
s += n
return s
a,b=map(int, input().split())
s=fn(a, b)
print(s)
输入2 3的结果为246