蟒蛇空心菱形与星号轮廓

时间:2016-09-20 23:09:38

标签: python python-3.x

我必须像这样建造一个空心钻石:

x(t,u,v) = ((cos(u)+2)cos(v))cos(a) + ((sin(u)+2)cos(t))sin(a)

y(u,v) = (cos(u)+2)sin(v)

z(t,u) = (sin(u)+2)sin(t)

这是我到目前为止所拥有的,

{{1}}

然而,使用我拥有的代码,我只获得了一半的钻石。

{{1}}

我应该使用代替循环而不是能够完成钻石吗?

4 个答案:

答案 0 :(得分:1)

你已经想出如何为每一行打印第一组星号;到目前为止工作很好。现在,您需要确定要打印的空间数量。让我们进行第一个循环,在 w 行的网格中打印 h 星号。

左侧需要 h 星号,右侧需要 h ;总共 2 * h 星号。这会在中间留下 s = w - 2 * h 空格。

因此,对于每一行,您需要打印...

  • h asterisks
  • s 空格
  • h 更多星号

这是否会让您对当前代码进行有用的更新?

答案 1 :(得分:0)

如你所说,建造一个空心钻石意味着可能如下:

  1. 带有完整星号的行(中间为0个空格)
  2. 中间有2个空格的行
  3. 中间有4个空格的行
  4. ...
  5. 中间有l-2个空格的行
  6. 中间有l-2个空格的行
  7. 中间有l-4个空格的行
  8. 中间有l-6个空格的行
  9. ...
  10. 带有完整星号的行(中间为l-l个空格)
  11. n是“步骤”,或者在每次迭代中“丢失”了多少个星号。 l是您广场的大小。

    因此,您的算法由两部分组成,即增加的空间和减少的空间。

    所以,你的算法应该是这样的

    for (spaces = 0; spaces < size/2 ; spaces = spaces + 1 ) 
        for (asterisk = 0; asterisk < size/2 - spaces; asterisk = asterisk + 1)
            print '*'
        for (space = 0; space < spaces*2; space = space + 1) 
            print ' '
        for (asterisk = 0; asterisk < size/2 - spaces; asterisk = asterisk + 1)
            print '*'
    for (spaces = size/2 - 1; spaces >= 0; spaces = spaces - 1) 
        # The same inner code as above
    

    我的目的是没有把python代码放在​​那里,所以你可以正确地完成你的功课;),但是一旦你理解了算法,这应该很容易。

答案 2 :(得分:0)

我不会从你那里偷走修理作业的快乐,但是这个练习非常有趣,所以我会给你另一个可能的版本给你一些想法:

def cool_diamond(w):
    r = []
    for y in range(w):
        s = '*' * (w - y)
        r.append("{0}{1}{0}".format(s, ''.join(['-' for x in range(2 * y)]), s))

    return '\n'.join(r + r[::-1])

for i in range(3, 6):
    print cool_diamond(i)
    print('-' * 80)

我强烈建议您先花点时间来修理你的!否则你不会从练习中学到任何东西。

一旦你修复了你的工作,你会对付出的努力感到非常满意,然后......就在那时,你可以考虑是否可以改善你的版本或重构。

快乐的编码!

******
**--**
*----*
*----*
**--**
******
--------------------------------------------------------------------------------
********
***--***
**----**
*------*
*------*
**----**
***--***
********
--------------------------------------------------------------------------------
**********
****--****
***----***
**------**
*--------*
*--------*
**------**
***----***
****--****
**********
--------------------------------------------------------------------------------

答案 3 :(得分:0)

在这里,请记住,只有输入的偶数才有效,否则程序将执行比该数字少的数字。我已经为您编写了代码。

n=int(input('Enter a number'))
if n%2==0:
    pass
else:
    n-=1
    print("Works only for even number,so decremented the number by 1")
k=n//2    
print(a*n)
for i in range(1,k):
    print(a*(k-i),end='')
    print(b*(c),end='')
    print(a*(k-i),end='')
    print()
    c=c+2
c=c-2
for i in range(1,k):
    print(a*(i),end='')
    print(b*(c),end='')
    print(a*(i),end='')
    print()
    c-=2
print(a*n)

您也可以将其用作功能。您可以轻松地进行转换。您可以同时使用while和for循环。
查看while循环。

a='*' ; b=' ';c=2
n=int(input('Enter a number'))
if n%2==0:
    pass
else:
    n-=1
    print("Works only for even number,so decremented the number by 1")
k=n//2
print(a*n)
i=1
while i<k:
    print(a*(k-i),end='')
    print(b*c,end='')
    print(a*(k-i),end='')
    print()
    c=c+2
    i+=1
c-=2
i=1
while i<k:
    print(a*i,end='')
    print(b*c,end='')
    print(a*i,end='')
    print()
    c-=2
    i+=1
print(a*n)

希望有帮助。

相关问题