使用字母打印 rangoli 图案的 Python 程序
假设我们有一个数字 n。我们必须创建 nxn 大小的字母 rangoli。n 必须在 1 和 26 之间,当 n 为 26 时,它将从 a 开始到 z 结束。
所以,如果输入像 5,那么输出将是
--------e--------------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
示例
让我们看下面的实现来更好地理解
def solve(n):for i in range(n-1,-1,-1):
for j in range(i):
print(end="--")
for j in range(n-1,i,-1):
print(chr(j+97),end="-")
for j in range(i,n):
if j != n-1:
print(chr(j+97),end="-")
else:
print(chr(j+97),end="")
for j in range(2*i):
print(end="-")
print()
for i in range(1,n):
for j in range(i):
print(end="--")
for j in range(n-1,i,-1):
print(chr(j+97),end="-")
for j in range(i,n):
if j != n-1:
print(chr(j+97),end="-")
else:
print(chr(j+97),end="")
for j in range(2*i):
print(end="-")
print()
n = 8
solve(n)
输入
8输出结果
--------------h--------------------------h-g-h------------
----------h-g-f-g-h----------
--------h-g-f-e-f-g-h--------
------h-g-f-e-d-e-f-g-h------
----h-g-f-e-d-c-d-e-f-g-h----
--h-g-f-e-d-c-b-c-d-e-f-g-h--
h-g-f-e-d-c-b-a-b-c-d-e-f-g-h
--h-g-f-e-d-c-b-c-d-e-f-g-h--
----h-g-f-e-d-c-d-e-f-g-h----
------h-g-f-e-d-e-f-g-h------
--------h-g-f-e-f-g-h--------
----------h-g-f-g-h----------
------------h-g-h------------
--------------h--------------
以上是 使用字母打印 rangoli 图案的 Python 程序 的全部内容, 来源链接: utcz.com/z/343692.html