Python 打印 V 和倒 V 图案的程序(Program to print V and inverted-V pattern)

倒 V 型模式:给定 n 的值,打印倒 V 型模式。
示例: 

输入:n = 5

输出 :

    E
   D D
  C   C
 B     B
A       A

输入:n = 7

输出 :

      G
     F F
    E   E
   D     D
  C       C
 B         B
A           A

下面是打印上述图案的程序:

# Python Implementation to print  
# the pattern  
  
# Function definition  
def pattern(n): 
    k = 0
    for i in range(n - 1, -1, -1): 
  
        # outer gap loop  
        for j in range(n - 1, k, -1): 
            print(' ', end = '') 
  
        # 65 is ASCII of 'A'  
        print(chr(i + 65), end = '') 
  
        # inner gap loop  
        for j in range(1, k * 2): 
            print(' ', end = '') 
        if i<n-1: 
            print(chr(i + 65), end = '') 
        print() 
        k += 1
  
# Driver Code  
  
# taking size from the user  
n = 5
  
# function calling  
pattern(n) 
  
# This code is contributed  
# by SamyuktaSHegde  

输出:

    E
   D D
  C   C
 B     B
A       A

时间复杂度: O(n 2 ),其中 n 表示给定的输入。

辅助空间: O(1),不需要额外的空间,因此为常数。

V 模式:给定 n 的值,打印 V 模式。

示例: 

输入:n = 5

输出:

E       E
 D     D
  C   C
   B B
    A

输入:n = 7

输出:

G           G
 F         F
  E       E
   D     D
    C   C
     B B
      A

下面是打印上述图案的程序: 

# Python3 Implementation to print 
# the pattern 
import math as mt 
  
# Function definition 
def pattern(n): 
  
    i = n - 1
    j = 1
    for i in range(n - 1, -1, -1): 
  
        # outer gap loop 
        for j in range(n - 1, i, -1): 
            print(' ', end = '') 
              
        # 65 is ASCII of 'A' 
        print(chr(i + 65), end = '') 
  
        # inner gap loop 
        for j in range(1, i * 2): 
            print(' ', end = '') 
  
        if (i >= 1): 
            print(chr(i + 65), end = '') 
        print() 
          
# Driver code 
  
# taking size from the user 
n = 5
  
# function calling 
pattern(n) 
  
# This code is contributed  
# by MOhit kumar 

输出: 

E       E
 D     D
  C   C
   B B
    A

时间复杂度: O(n 2 ),其中 n 表示给定的输入。

辅助空间: O(1),不需要额外的空间,因此为常数。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值