Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 空心方形内方形图案 这是我的程序所做的:以 N< /代码>为输入,使用 2 ^ {n+1 } -1代码>计算方边长,然后在每个正方形的顶点放置在前一方的中间的图案中打印“n”方块。以下是一些示例输出:_Python_Python 3.x_Algorithm - Fatal编程技术网

Python 空心方形内方形图案 这是我的程序所做的:以 N< /代码>为输入,使用 2 ^ {n+1 } -1代码>计算方边长,然后在每个正方形的顶点放置在前一方的中间的图案中打印“n”方块。以下是一些示例输出:

Python 空心方形内方形图案 这是我的程序所做的:以 N< /代码>为输入,使用 2 ^ {n+1 } -1代码>计算方边长,然后在每个正方形的顶点放置在前一方的中间的图案中打印“n”方块。以下是一些示例输出:,python,python-3.x,algorithm,Python,Python 3.x,Algorithm,输入:2 输出: ####### #..#..# #.#.#.# ##...## #.#.#.# #..#..# ####### ############### #......#......# #.....#.#.....# #....#...#....# #...#######...# #..##.....##..# #.#.#.....#.#.# ##..#.....#..## #.#.#.....#.#.# #..##.....##..# #...#######...# #....#..

输入:
2

输出:

#######
#..#..#
#.#.#.#
##...##
#.#.#.#
#..#..#
#######
###############
#......#......#
#.....#.#.....#
#....#...#....#
#...#######...#
#..##.....##..#
#.#.#.....#.#.#
##..#.....#..##
#.#.#.....#.#.#
#..##.....##..#
#...#######...#
#....#...#....#
#.....#.#.....#
#......#......#
###############
输入:
3

输出:

#######
#..#..#
#.#.#.#
##...##
#.#.#.#
#..#..#
#######
###############
#......#......#
#.....#.#.....#
#....#...#....#
#...#######...#
#..##.....##..#
#.#.#.....#.#.#
##..#.....#..##
#.#.#.....#.#.#
#..##.....##..#
#...#######...#
#....#...#....#
#.....#.#.....#
#......#......#
###############
我已经尝试解决这个问题有一段时间了,问题是我不能让代码像任何给定数字的算法一样工作,下面是我的代码,它只适用于
n=1或2

lenght = int(input('Please input an integer : '))
n=2**(lenght+1)-1
mid=int((n-1)/2)-1


print('#'*n)

i=-1
cnt1 = 0
midflag=1

while cnt1 < (n-2):
    print('#',end='')

    a='.'*(mid)+'#'
    if  lenght==1:
        a='.' 
    print (a,end='')
    print ('.'*(i),end='')

    if i==-1:
        print (a[::-1][1:],end='')
    else:
        print (a[::-1],end='')

    if midflag*mid>0:
        mid-=1
        i+=2
    else:
        mid+=1
        i-=2
        midflag=0

    cnt1 += 1

    print('#')

print('#'*n,end='')
lenght=int(输入('请输入一个整数:'))
n=2**(长度+1)-1
mid=int((n-1)/2)-1
打印(“#”*n)
i=-1
cnt1=0
中旗=1
而cnt1<(n-2):
打印(“#”,结束=”)
a='.*(中间)+'#'
如果长度==1:
a='。'
打印(a,结束=“”)
打印('.*(i),结束='')
如果i==-1:
打印(a[:-1][1:],结束=“”)
其他:
打印(a[:-1],结束=“”)
如果midflag*mid>0:
中-=1
i+=2
其他:
中位数+=1
i-=2
中间标志=0
cnt1+=1
打印(“#”)
打印('#'*n,end='')

关于如何使它适用于任何给定的数字,有什么建议吗?

当然可以这样做,但我发现先在内存中创建网格,然后在该网格中“绘制”形状更容易。这样,您就不必一行一行地生成最终输出,而必须同时考虑所有形状

所以我建议创建一个矩阵,最初只有点,没有散列。然后迭代你必须绘制的不同形状(正方形或菱形),并将相应的散列放在矩阵的正确坐标处

请注意,输出在水平和垂直方向上都是对称的,因此您可以只关注矩阵的一个象限,并仅在最后通过沿X和Y轴镜像该象限,从该象限生成其他象限

下面是采用这种方法的代码:

length = int(input('Please input an integer:'))
# let n be the half of the total width, including the centre file
n = 2**length 

# create an empty grid, just for the bottom-right quadrant
grid = [['.']*n for _ in range(n)]

# Draw each of the partial shapes in that quadrant
for shape in range(length):
    if shape % 2: # draw one side of a diamond
        for i in range(1, n):
            grid[i-1][n-1-i] = '#'
        n //= 2 # adjust the size for the next square (if any)
    else: # draw 2 half-sides of a square
        for i in range(n):
            grid[n-1][i] = '#' # horizontal side
        for line in grid[:n]:
            line[n-1] = '#'    # vertical side

# Create the other 3 quadrants by mirroring, and join to a string 
print("\n".join(["".join(line[:0:-1] + line) for line in grid[:0:-1] + grid]))