编写一个函数,产生数列a,aa,aaa,aaaa,aa...a的第n项,a和n为形参,第n项为返回值
时间: 2023-12-09 14:03:58 浏览: 72
求!+!+!++n!-过程和函数
以下是一个 Python 的实现,函数名为 `generate_sequence`:
```python
def generate_sequence(a, n):
"""
产生数列a,aa,aaa,aaaa,aa...a的第n项
"""
if n <= 0:
return None
result = 0
for i in range(n):
result += a * (10 ** i)
return result
```
使用示例:
```python
>>> generate_sequence(5, 3)
555
>>> generate_sequence(7, 5)
77777
>>> generate_sequence(9, 1)
9
```
阅读全文