Dado un número entero N , la tarea es imprimir el patrón del alfabeto Z como se indica a continuación:
1 2 3 * * * N * * * 3 2 1 2 3 * * * N
Ejemplos:
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
Acercarse:
- Imprime la primera fila con números del 1 al N.
- Luego, desde la fila 2 a la (N-1) , imprima 2 * (N – índice – 1) veces los espacios en blanco seguidos del elemento final que es índice – 1 .
- Imprime la última fila con números del 1 al N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> 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 <stdio.h> // 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
<?php // PHP implementation of the approach // Function to print the desired // Alphabet Z Pattern function alphabetPattern($N) { $index; $side_index; $size; // Declaring the values of Right, // Left and Diagonal values $Top = 1; $Bottom = 1; $Diagonal = $N - 1; // Loop for printing the first row for ($index = 0; $index < $N; $index++) echo $Top++ . " "; echo "\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++) echo " "; // Printing the diagonal values echo $Diagonal--; echo "\n"; } // Loop for printing the last row for ($index = 0; $index < $N; $index++) echo $Bottom++ . " "; } // Driver Code // Number of rows $N = 5; alphabetPattern($N); // This code is contributed by Akanksha Rai
Javascript
<script> // JavaScript implementation of the approach // Function to print the desired // Alphabet Z Pattern function alphabetPattern(N) { var index, side_index, size; // Declaring the values of Right, // Left and Diagonal values var Top = 1, Bottom = 1, Diagonal = N - 1; // Loop for printing the first row for (index = 0; index < N; index++) { document.write(Top + " "); Top++; } document.write("<br>"); // 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++) document.write(" "); // Printing the diagonal values document.write(Diagonal); Diagonal--; document.write("<br>"); } // Loop for printing the last row for (index = 0; index < N; index++) { document.write(Bottom + " "); Bottom++; } } // Driver Code // Number of rows var N = 5; alphabetPattern(N); </script>
Producción:
1 2 3 4 5 4 3 2 1 2 3 4 5
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por VISHAL_PERIYASAMY_R y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA