倒 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),不需要额外的空间,因此为常数。