gpt4 book ai didi

python - 在 Python 中解决回文 'Triangle Quest' 难题

转载 作者:太空狗 更新时间:2023-10-29 20:47:14 28 4
gpt4 key购买 nike

我正在尝试解决这个编程难题:

You are given a positive integer N (0 < N < 10). Your task is to print a palindromic triangle of size N.

For example, a palindromic triangle of size 5 is:

1
121
12321
1234321
123454321

You can't take more than two lines. You have to complete the code using exactly one print statement.

Note: Using anything related to strings will give a score of 0. Using more than one for-statement will give a score of 0.

我只能想到“愚蠢”的方式来做到这一点:

for i in range(1, N+1):
print([0, 1, 121, 12321, 1234321, 123454321, 12345654321, 1234567654321, 123456787654321, 12345678987654321][i])

有没有更优雅的解决方案?

最佳答案

我最终做了以下事情(感谢@raina77ow 的想法):

for i in range(1, N+1):
print((111111111//(10**(9-i)))**2)

关于python - 在 Python 中解决回文 'Triangle Quest' 难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494247/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com