数字金字塔

Posted wang102030

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数字金字塔相关的知识,希望对你有一定的参考价值。

技术分享图片

num = eval(input("请输入一个整数:"))
for i in range(1, num + 1):
    for j in range(num-i, 0, -1):
        print(" ", end="	")
    for j in range(i, 1, -1):
        print(j, end="	")
    for j in range(1, i+1):
        print(j, end="	")
    print(" ")

技术分享图片

row = int(input("请输入行数:"))
num = 1
sum = 0
for i in range(1, row + 1):
    for j in range(row - i, 0, -1):
        print(" ", end="	")

    for j in range(1, i + 1):
        print(num, end="	")
        num *= 2
        sum = num
    num = 1
    sum //= 2
    for j in range(2, i + 1):
        print(sum//2, end="	")
        sum //= 2

    print("")

 技术分享图片

pattern = input("请输入你要选择的模式:")
while True:
    if pattern == "A":
        row = eval(input("请输入行数:"))
        for i in range(1, row + 1):
            for j in range(1, i+1):
                print(j, end=" ")
            print("")

    elif pattern == "B":
        row = eval(input("请输入行数:"))
        for i in range(1, row + 1):
            for j in range(1, row - i + 2):
                print(j, end=" ")
            print("")

    elif pattern == "C":
        row = eval(input("请输入行数:"))
        for i in range(1, row + 1):
            for j in range(row - i, 0, -1):
                print(" ", end=" ")
            for j in range(i, 0, -1):
                print(j, end=" ")
            print("")

    elif pattern == "D":
        row = eval(input("请输入行数:"))
        for i in range(1, row + 1):
            for j in range(1, i):
                print(" ", end=" ")
            for j in range(1, row - i + 2 ):
                print(j, end=" ")
            print("")

    elif pattern == "退出":
        break
    else:
        pattern = input("输入错误,请从新输入")

 



以上是关于数字金字塔的主要内容,如果未能解决你的问题,请参考以下文章

用python打印数字金字塔

求镂空的倒金字塔java代码....金字塔可用*代替! 谢了

Python输出数字金字塔

java n的金字塔

怎么用java打印金字塔

用java把1-100这100个数字按顺序排成金字塔形状,尽量用最简单的代码实现