You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成指定格式的星号组合字符串输出?

Got it, let's figure out how to generate that exact string you're targeting. Your current code creates the basic increasing sequence of centered asterisks, but the desired output combines those star groups in specific pairs and sequences, joined by + signs.

First, let's break down the target into its component parts. The final string is made by joining these segments with +:

  • *
  • **** **** ***
  • *** *** *****
  • ** ** *******
  • * * *****
  • ** ** ***
  • *** *** *
  • **** ****

You can either construct these parts directly (simple and straightforward for a fixed pattern) or generate them with loops if you want to avoid hardcoding. Here's both approaches:

Approach 1: Direct Construction (Simplest)

Just define the list of segments and join them:

target_segments = [
    " * ",
    "**** **** ***",
    "*** *** *****",
    "** ** *******",
    "* * *****",
    "** ** ***",
    "*** *** *",
    "**** ****"
]
result = " + ".join(target_segments)
print(result)

Approach 2: Generate Segments with Loops

If you prefer to generate the segments programmatically (to see the underlying pattern), notice that most segments follow a [x] [x] [y] structure where x and y follow predictable sequences:

# Initialize the list with the first segment
segments = [" * "]

# Generate the first set of middle segments (2-4)
for x, y in zip([4, 3, 2], [3, 5, 7]):
    segments.append(f"{x*'*'} {x*'*'} {y*'*'}")

# Generate the second set of middle segments (5-7)
for x, y in zip([1, 2, 3], [5, 3, 1]):
    segments.append(f"{x*'*'} {x*'*'} {y*'*'}")

# Add the final segment
segments.append("**** ****")

# Join all segments with " + "
result = " + ".join(segments)
print(result)

Either way, running this code will output exactly the string you're aiming for:

* + **** **** *** + *** *** ***** + ** ** ******* + * * ***** + ** ** *** + *** *** * + **** ****

内容的提问来源于stack exchange,提问作者tabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:35:15