用递归函数输出 A1 AB21 ABC321 ABCD4321 ABCDE54321
时间: 2024-06-13 22:07:40 浏览: 13
以下是用递归函数输出A1 AB21 ABC321 ABCD4321 ABCDE54321的代码实现:
```python
def print_alphabet(num):
if num == 1:
print('A', end='')
else:
print_alphabet(num-1)
print(chr(64+num), end='')
def print_number(num):
if num == 1:
print('1', end='')
else:
print(num-1, end='')
print_number(num-1)
for i in range(1, 6):
print_alphabet(i)
print_number(i)
print()
```
相关问题
用递归函数输出斐波那契数列
好的,以下是用递归函数输出斐波那契数列的 Python 代码示例:
```python
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
# 输出前10个斐波那契数列
for i in range(10):
print(fibonacci(i))
```
这段代码定义了一个名为 `fibonacci` 的递归函数,用于计算斐波那契数列的第 `n` 项。当 `n` 小于等于1时,直接返回 `n`。否则,递归调用 `fibonacci` 函数计算第 `n-1` 和 `n-2` 项,并将它们相加返回。
在主程序中,我们通过循环调用 `fibonacci` 函数输出前10个斐波那契数列。
c语言用递归函数输出菱形
好的,我来为您解答。下面是 C 语言递归函数输出菱形的代码:
```
#include <stdio.h>
void print_lines(int n, int i) {
if (i == n+1) return; // 递归退出条件
int j;
for (j = 1; j <= n - i + 1; j++) printf(" "); // 输出空格
for (j = 1; j <= 2*i-1; j++) printf("*"); // 输出星号
printf("\n"); // 换行
print_lines(n, i+1); // 递归调用
for (j = 1; j <= n - i + 1; j++) printf(" "); // 输出空格
for (j = 1; j <= 2*i-1; j++) printf("*"); // 输出星号
printf("\n"); // 换行
}
void rhombus(int n) {
int i;
for (i = 1; i <= n; i++) {
print_lines(n, i);
}
for (i = n-1; i >= 1; i--) {
print_lines(n, i);
}
}
int main() {
int n;
printf("请输入菱形的行数:");
scanf("%d", &n);
rhombus(n);
return 0;
}
```
希望能对您有所帮助!同时,您还有其他问题吗?
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)