如何在python中使用下面的源代码制作三角形x

时间:2016-09-05 16:56:18

标签: python algorithm pascal

您好我想问大家...... 像这样... 我想制作一个三角形X或*,如下所示:

    *
   ***
  *****
 *******
*********

我的算法是这样的:

for y:=1 to i do
    for x:=1 to j do
       for j-x to 1 do write(' ');
       for i to 2*(x-1)+1 do write('*');

有人能告诉我上面的python的源代码是怎么样的? 谢谢你的回答

3 个答案:

答案 0 :(得分:3)

您可以这样做:

def triangle(lines):
    for i in range(lines):
        print(' '*(lines-i) + '*'*(i*2+1))

triangle(5)

按预期输出

     *
    ***
   *****
  *******
 *********

答案 1 :(得分:1)

我有一些代码要做到这里,也许这可以帮助你:)。

def pascals_triangle(order):
    """
    :brief:         Compute the line of pascal's triangle with order 'order'
                    |       line          | order |
                    |---------------------|-------|
                    |         1           |   0   |
                    |       1   1         |   1   |
                    |     1   2   1       |   2   |
                    |    1   3   3   1    |   3   |
                    | 1   4   6   4   1   |   4   |
    :param order:   order of the line in pascal's triangle 
                    (starting with 0, which returns just [1])
    :return:        a list of the pascal's triangle line of order 'order'
    """
    line = [1]
    for k in xrange(order):
        line.append(line[k] * (order - k) / (k + 1))
    return line

答案 2 :(得分:1)

这是一个可能的解决方案:

def print_pascals_triangle(levels, debug_char=None):
    triangle = []

    for order in range(levels):
        line = [debug_char] if debug_char is not None else [1]
        for k in range(order):
            if debug_char is None:
                value = line[k] * (order - k) / (k + 1)
                line.append(value)
            else:
                line.append(debug_char)
        triangle.append(line)

    def format_row(row):
        return ' '.join(map(str, row))

    triangle_width = len(format_row(triangle[-1]))
    for row in triangle:
        print(format_row(row).center(triangle_width))

如果你想要真正的pascal三角形,你可以像这样使用它:

for level in range(1, 8):
    print_pascals_triangle(level)
    print('-' * 80)

你会得到:

1
--------------------------------------------------------------------------------
 1 
1 1
--------------------------------------------------------------------------------
  1  
 1 1 
1 2 1
--------------------------------------------------------------------------------
   1   
  1 1  
 1 2 1 
1 3 3 1
--------------------------------------------------------------------------------
    1    
   1 1   
  1 2 1  
 1 3 3 1 
1 4 6 4 1
--------------------------------------------------------------------------------
      1      
     1 1     
    1 2 1    
   1 3 3 1   
  1 4 6 4 1  
1 5 10 10 5 1
--------------------------------------------------------------------------------
       1        
      1 1       
     1 2 1      
    1 3 3 1     
   1 4 6 4 1    
 1 5 10 10 5 1  
1 6 15 20 15 6 1
--------------------------------------------------------------------------------

否则,您可以使用其他字符伪造数字,如下所示:

for level in range(1, 8):
    print_pascals_triangle(level, debug_char='*')
    print('-' * 80)

你会得到这个:

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