倒 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

下面是打印上述图案的程序:
// C++ Implementation to print the pattern
#include <bits/stdc++.h>
using namespace std;
// Function definition
void pattern(int n)
{
int i, j, k = 0;
for (i = n - 1; i >= 0; i--) {
// outer gap loop
for (j = n - 1; j > k; j--) {
cout << " ";
}
// 65 is ASCII of 'A'
cout << char(i + 65);
// inner gap loop
for (j = 1; j < (k * 2); j++)
cout << " ";
if (i < n - 1)
cout << char(i + 65);
cout << "\n";
k++;
}
}
// Driver code
int main()
{
// taking size from the user
int n = 5;
// function calling
pattern(n);
return 0;
}
输出:
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

下面是打印上述图案的程序:
// C++ Implementation to print the pattern
#include <bits/stdc++.h>
using namespace std;
// Function definition
void pattern(int n)
{
int i, j;
for (i = n - 1; i >= 0; i--) {
// outer gap loop
for (j = n - 1; j > i; j--) {
cout << " ";
}
// 65 is ASCII of 'A'
cout << char(i + 65);
// inner gap loop
for (j = 1; j < (i * 2); j++)
cout << " ";
if (i >= 1)
cout << char(i + 65);
cout << "\n";
}
}
// Driver code
int main()
{
// taking size from the user
int n = 5;
// function calling
pattern(n);
return 0;
}
输出:
E E
D D
C C
B B
A

时间复杂度: O(n 2 ),其中 n 表示给定的输入。
辅助空间: O(1),不需要额外的空间,因此为常数。
2万+

被折叠的 条评论
为什么被折叠?



