给定整数N ,任务是打印字母Z模式,如下所示:
1 2 3 * * * N
*
*
*
3
2
1 2 3 * * * N
例子:
Input: N = 5
Output:
1 2 3 4 5
4
3
2
1 2 3 4 5
Input: N = 4
Output:
1 2 3 4
3
2
1 2 3 4
方法:
- 用1到N的数字打印第一行。
- 然后从第二行到第(N-1)行,打印2 *(N – index – 1)乘以空格,后跟结束元素,即index – 1 。
- 用1到N的数字打印最后一行。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the desired
// Alphabet Z Pattern
void alphabetPattern(int N)
{
int index, side_index, size;
// Declaring the values of Right,
// Left and Diagonal values
int Top = 1, Bottom = 1, Diagonal = N - 1;
// Loop for printing the first row
for (index = 0; index < N; index++)
cout << Top++ << " ";
cout << endl;
// Main Loop for the rows from (2 to n-1)
for (index = 1; index < N - 1; index++) {
// Spaces for the diagonals
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
cout << " ";
// Printing the diagonal values
cout << Diagonal--;
cout << endl;
}
// Loop for printing the last row
for (index = 0; index < N; index++)
cout << Bottom++ << " ";
}
// Driver Code
int main()
{
// Number of rows
int N = 5;
alphabetPattern(N);
return 0;
}
C
// C implementation of the approach
#include
// Function to print the desired
// Alphabet Z Pattern
void alphabet_Z_Pattern(int N)
{
int index, side_index, size;
// Declaring the values of Right,
// Left and Diagonal values
int Top = 1, Bottom = 1, Diagonal = N - 1;
// Loop for printing the first row
for (index = 0; index < N; index++)
printf("%d ", Top++);
printf("\n");
// Main Loop for the rows from (2 to n-1)
for (index = 1; index < N - 1; index++) {
// Spaces for the diagonals
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
printf(" ");
// Printing the diagonal values
printf("%d", Diagonal--);
printf("\n");
}
// Loop for printing the last row
for (index = 0; index < N; index++)
printf("%d ", Bottom++);
}
// Driver Code
int main()
{
// Size of the Pattern
int N = 5;
alphabet_Z_Pattern(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to print the desired
// Alphabet Z Pattern
static void alphabetPattern(int N)
{
int index, side_index;
// Declaring the values of Right,
// Left and Diagonal values
int Top = 1, Bottom = 1, Diagonal = N - 1;
// Loop for printing the first row
for (index = 0; index < N; index++)
System.out.print(Top++ + " ");
System.out.println();
// Main Loop for the rows from (2 to n-1)
for (index = 1; index < N - 1; index++)
{
// Spaces for the diagonals
for (side_index = 0;
side_index < 2 * (N - index - 1);
side_index++)
System.out.print(" ");
// Printing the diagonal values
System.out.print(Diagonal--);
System.out.println();
}
// Loop for printing the last row
for (index = 0; index < N; index++)
System.out.print(Bottom++ + " ");
}
// Driver Code
public static void main(String args[])
{
// Number of rows
int N = 5;
alphabetPattern(N);
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python 3 implementation of the approach
# Function to print the desired
# Alphabet Z Pattern
def alphabetPattern(N):
# Declaring the values of Right,
# Left and Diagonal values
Top, Bottom, Diagonal = 1, 1, N - 1
# Loop for printing the first row
for index in range(N):
print(Top, end = ' ')
Top += 1
print()
# Main Loop for the rows from (2 to n-1)
for index in range(1, N - 1):
# Spaces for the diagonals
for side_index in range(2 * (N - index - 1)):
print(' ', end = '')
# Printing the diagonal values
print(Diagonal, end = '')
Diagonal -= 1
print()
# Loop for printing the last row
for index in range(N):
print(Bottom, end = ' ')
Bottom += 1
# Driver Code
# Number of rows
N = 5
alphabetPattern(N)
# This code is contributed
# by SamyuktaSHegde
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the desired
// Alphabet Z Pattern
static void alphabetPattern(int N)
{
int index, side_index;
// Declaring the values of Right,
// Left and Diagonal values
int Top = 1, Bottom = 1, Diagonal = N - 1;
// Loop for printing the first row
for (index = 0; index < N; index++)
Console.Write(Top++ + " ");
Console.WriteLine();
// Main Loop for the rows from (2 to n-1)
for (index = 1; index < N - 1; index++)
{
// Spaces for the diagonals
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
Console.Write(" ");
// Printing the diagonal values
Console.Write(Diagonal--);
Console.WriteLine();
}
// Loop for printing the last row
for (index = 0; index < N; index++)
Console.Write(Bottom++ + " ");
}
// Driver Code
public static void Main()
{
// Number of rows
int N = 5;
alphabetPattern(N);
}
}
// This code is contributed
// by Akanksha Rai
PHP
Javascript
输出:
1 2 3 4 5
4
3
2
1 2 3 4 5
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。