给定整数N ,任务是打印字母H模式,如下所示:
1 N
2 *
3 3
* 2
N * 3 2 1
* 2
3 3
2 *
1 N
例子:
Input: N = 3
Output:
1 3
2 2
3 2 1
2 2
1 3
Input: N = 4
Output:
1 4
2 3
3 2
4 3 2 1
3 2
2 3
1 4
方法:
- 打印Left值,并保留2 *(index – 1)空格并打印Right值。
- 用N到1的数字打印第N行。
- 重复步骤(2 * N)– 1次以打印所需的H图案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the desired
// Alphabet H Pattern
void alphabetPattern(int N)
{
// Declaring the values of left,
// middle, right side
int left = 0, middle = N - 1, right = N + 1;
// Main Row Loop
for (int row = 0; row < 2 * N - 1; row++) {
// Condition for the left Values
if (row < N)
cout << ++left;
else
cout << --left;
// Loop for the middle values
for (int col = 1; col < N - 1; col++) {
// Condition for the middleValues
if (row != N - 1)
// Two spaces for perfect alignment
cout << " "
<< " ";
else
cout << " " << middle--;
}
// Condition for the right Values
if (row < N)
cout << " " << --right;
else
cout << " " << ++right;
cout << endl;
}
}
// Driver Code
int main()
{
// Size of the Pattern
int N = 4;
alphabetPattern(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to print the desired
// Alphabet H Pattern
static void alphabetPattern(int N)
{
// Declaring the values of left,
// middle, right side
int left = 0, middle = N - 1, right = N + 1;
// Main Row Loop
for (int row = 0; row < 2 * N - 1; row++) {
// Condition for the left Values
if (row < N)
System.out.print( ++left);
else
System.out.print(--left);
// Loop for the middle values
for (int col = 1; col < N - 1; col++) {
// Condition for the middleValues
if (row != N - 1)
// Two spaces for perfect alignment
System.out.print( " ");
else
System.out.print( " " +middle--);
}
// Condition for the right Values
if (row < N)
System.out.print( " " +--right);
else
System.out.print( " " + ++right);
System.out.println();
}
}
// Driver Code
public static void main(String[] args) {
// Size of the Pattern
int N = 4;
alphabetPattern(N);
// This code is contributed by Rajput-Ji
}
}
Python3
# Python3 implementation of the approach
# Function to print the desired
# Alphabet H Pattern
def alphabetPattern(N):
# Declaring the values of left,
# middle, right side
left, middle, right = 0, N - 1, N + 1
# Main Row Loop
for row in range(0, 2 * N - 1):
# Condition for the left Values
if row < N:
left += 1
print(left, end = "")
else:
left -= 1
print(left, end = "")
# Loop for the middle values
for col in range(1, N - 1):
# Condition for the middleValues
if row != N - 1:
# Two spaces for perfect alignment
print(" ", end = " ")
else:
print(" " + str(middle), end = "")
middle -= 1
# Condition for the right Values
if row < N:
right -= 1
print(" " + str(right), end = "")
else:
right += 1
print(" " + str(right), end = "")
print()
# Driver Code
if __name__ == "__main__":
# Size of the Pattern
N = 4
alphabetPattern(N)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the desired
// Alphabet H Pattern
static void alphabetPattern(int N)
{
// Declaring the values of left,
// middle, right side
int left = 0, middle = N - 1, right = N + 1;
// Main Row Loop
for (int row = 0; row < 2 * N - 1; row++)
{
// Condition for the left Values
if (row < N)
Console.Write( ++left);
else
Console.Write(--left);
// Loop for the middle values
for (int col = 1; col < N - 1; col++)
{
// Condition for the middleValues
if (row != N - 1)
// Two spaces for perfect alignment
Console.Write( " ");
else
Console.Write( " " + middle--);
}
// Condition for the right Values
if (row < N)
Console.Write( " " + --right);
else
Console.Write( " " + ++right);
Console.WriteLine();
}
}
// Driver Code
public static void Main(String[] args)
{
// Size of the Pattern
int N = 4;
alphabetPattern(N);
}
}
// This code is contributed by
// PrinciRaj1992
PHP
Javascript
输出:
1 4
2 3
3 2
4 3 2 1
3 2
2 3
1 4
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。