=======[2.out]========= Expected | Yours < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ============================== ========[sample.out]========= Expected | Yours * | * *** | * * * * | * * * * | * * * ********* | * * * * * * * * * * * * | * * * * | * *** | * * * * | * * * * | * * * *** *** *** | * * * * * * * * * * * | * * * * * * * * * * * * | * * * * * * * *************************** | * * * * * * * * * * *
以下是重写的 Python 代码,用于生成符合要求的递归图形: ```python def draw_pattern(n): if n == 1: return ["*"] prev_pattern = draw_pattern(n - 1) width = 3 (n - 1) new_pattern = [] # 上半部分 for line in prev_pattern: padding = " " * ((width - len(line)) // 2) new_pattern.append(padding + line + padding) # 中间部分 for line in prev_pattern: new_pattern.append(line + " " * (width - len(line)) + line + " " * (width - len(line)) + line) # 下半部分 for line in prev_pattern: padding = " " * ((width - len(line)) // 2) new_pattern.append(padding + line + padding) return new_pattern n = int(input()) pattern = draw_pattern(n) for line in pattern: print(line) ``` 代码解释: 1. 递归函数 `draw_pattern`: - 基本情况:当 `n` 等于 1 时,直接返回包含单个星号 `"*"` 的列表。 - 递归步骤: - 首先调用 `draw_pattern(n - 1)` 生成 `n - 1` 时的图形。 - 计算当前图形的宽度 `width = 3 (n - 1)`。 - 上半部分:对于 `n - 1` 图形的每一行,在其前后添加适当数量的空格,使其居中,然后添加到新图形列表中。 - 中间部分:将 `n - 1` 图形的每一行复制三次,中间用与宽度相关的空格分隔,添加到新图形列表中。 - 下半部分:和上半部分处理方式相同,再次添加 `n - 1` 图形的每一行(添加空格使其居中)到新图形列表中。 2. 主程序: - 读取用户输入的 `n`。 - 调用 `draw_pattern(n)` 生成图形。 - 遍历生成的图形列表,逐行打印输出。 复杂度分析: - 时间复杂度:$O(3^n)$,因为图形的行数和列数都随着 `n` 的增大呈指数级增长。 - 空间复杂度:$O(3^n)$,主要用于存储生成的图形。 这个代码能够根据输入的 `n` 正确生成对应的递归图形。 [2026-01-07 12:25:50 | AI问答 | 591点数解答]
- [阿里通义] ========[cyj01.out]========= Expected | Yours 27604 | 11972450 ============================== ========[cyj02.out]========= Expected | Yours 5289 | 530127 ============================== ========[cyj03.out]========= Expected | Yours 343 | 12817 ============================== ========[cyj04.out]========= Expected | Yours 1403 | 1048464 ============================== ========[cyj05.out]========= Expected (1107点数解答 | 2026-01-08 12:17:38)20
- [阿里通义] ========[cyj01.out]========= Expected | Yours 27604 | 11972450 ============================== ========[cyj02.out]========= Expected | Yours 5289 | 530127 ============================== ========[cyj03.out]========= Expected | Yours 343 | 12817 ============================== ========[cyj04.out]========= Expected | Yours 1403 | 1048464 ============================== ========[cyj05.out]========= Expected (1316点数解答 | 2026-01-08 12:18:39)27
- [阿里通义] ========[cyj01.out]========= Expected | Yours 27604 | 11972450 ============================== ========[cyj02.out]========= Expected | Yours 5289 | 530127 ============================== ========[cyj03.out]========= Expected | Yours 343 | 12817 ============================== ========[cyj04.out]========= Expected | Yours 1403 | 1048464 ============================== ========[cyj05.out]========= Expected (980点数解答 | 2026-01-08 12:21:23)23
- [阿里通义] wangyc 错误信息 ========[cyj01.out]========= Expected | Yours 27604 | 11972450 ============================== ========[cyj02.out]========= Expected | Yours 5289 | 530127 ============================== ========[cyj03.out]========= Expected | Yours 343 | 12817 ============================== ========[cyj04.out]========= Expected | Yours 1403 | 1048464 ============================== ========[cyj05.out]====(1482点数解答 | 2026-01-08 12:25:33)29
- [阿里通义] 错误信息 ========[cyj01.out]========= Expected | Yours 27604 | 11972450 ============================== ========[cyj02.out]========= Expected | Yours 5289 | 530127 ============================== ========[cyj03.out]========= Expected | Yours 343 | 12817 ============================== ========[cyj04.out]========= Expected | Yours 1403 | 1048464 ============================== ========[cyj05.out]========= Exp(1808点数解答 | 2026-01-08 12:28:14)23
- [DeepSeek] ========[sample.out]========= Expected | Yours ******** | * * * * * * * * * * * * | * * * * ** ** | * * * * * * | * * **** | * * * * > * * * * * * ** | * * < ============================== ========[star1.out]========= Expected | Yours ** | * * * | * ============================== ========[star2.out]========= Expected (439点数解答 | 2026-01-06 17:54:07)30
- [DeepSeek] wangyc 错误信息 ========[sample.out]========= Expected | Yours ******** | * * * * * * * * * * * * | * * * * ** ** | * * * * * * | * * **** | * * * * > * * * * * * ** | * * < ============================== ========[star1.out]========= Expected | Yours ** | * * * | * ============================== ========[star2.out]========= (1026点数解答 | 2026-01-06 17:56:11)25
- [DeepSeek] =======[cyj0.out]========= Expected | Yours 1 | 1 1 \ 1 ============================== ========[cyj1.out]========= Expected | Yours 1 2 3 4 5 6 8 9 10 12 | 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 | 15 16 18 20 24 25 16 \ 16 ============================== ========[cyj2.out]========= Expected | (221点数解答 | 2026-01-12 17:56:45)27
- [阿里通义] ========[cyj0.out]========= Expected | Yours 1 | 1 1 \ 1 ============================== ========[cyj1.out]========= Expected | Yours 1 2 3 4 5 6 8 9 10 12 | 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 | 15 16 18 20 24 25 16 \ 16 ============================== ========[cyj2.out]========= Expected |(1192点数解答 | 2026-01-14 12:26:46)39
- [阿里通义] ========[cyj0.out]========= Expected | Yours 1 | 1 1 \ 1 ============================== ========[cyj1.out]========= Expected | Yours 1 2 3 4 5 6 8 9 10 12 | 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 | 15 16 18 20 24 25 16 \ 16 ============================== ========[cyj2.out]========= Expected |(720点数解答 | 2026-01-14 12:28:54)20
- [阿里通义] ========[cyj0.out]========= Expected | Yours 1 | 1 1 \ 1 ============================== ========[cyj1.out]========= Expected | Yours 1 2 3 4 5 6 8 9 10 12 | 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 | 15 16 18 20 24 25 16 \ 16 ============================== ========[cyj2.out]========= Expected |(1146点数解答 | 2026-01-14 12:33:46)35
- [阿里通义] ========[cyj0.out]========= Expected | Yours 1 | 1 1 \ 1 ============================== ========[cyj1.out]========= Expected | Yours 1 2 3 4 5 6 8 9 10 12 | 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 | 15 16 18 20 24 25 16 \ 16 ============================== ========[cyj2.out]========= Expected |(1030点数解答 | 2026-01-14 12:35:51)21