Dado un número entero N , la tarea es imprimir el patrón del alfabeto H como se indica a continuación:
1 N 2 * 3 3 * 2 N * 3 2 1 * 2 3 3 2 * 1 N
Ejemplos:
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
Acercarse:
- Imprima el valor de la izquierda y deje 2 * (índice – 1) espacios en blanco e imprima el valor de la derecha.
- Imprime la N-ésima fila con N a 1 número.
- Repita el paso uno para (2 * N) – 1 vez para imprimir el patrón H deseado.
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 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
<?php // PHP implementation of the approach // Function to print the desired // Alphabet H Pattern function alphabetPattern($N) { // Declaring the values of left, // middle, right side $left = 0; $middle = $N - 1; $right = $N + 1; // Main Row Loop for ($row = 0; $row < 2 * $N - 1; $row++) { // Condition for the left Values if ($row < $N) echo (++$left); else echo (--$left); // Loop for the middle values for ($col = 1; $col < $N - 1; $col++) { // Condition for the middleValues if ($row != $N - 1) // Two spaces for perfect alignment echo " "." "; else echo " ".($middle--); } // Condition for the right Values if ($row < $N) echo " ".(--$right); else echo " ".(++$right); echo "\n"; } } // Driver Code // Size of the Pattern $N = 4; alphabetPattern($N); // This code is contributed by mits ?>
Javascript
<script> // JavaScript implementation // of the approach // Function to print the desired // Alphabet H Pattern function alphabetPattern(N) { // Declaring the values of left, // middle, right side var left = 0, middle = N - 1, right = N + 1; // Main Row Loop for (var row = 0; row < 2 * N - 1; row++) { // Condition for the left Values if (row < N) { ++left; document.write(left); } else { --left; document.write(left); } // Loop for the middle values for (var col = 1; col < N - 1; col++) { // Condition for the middleValues if (row != N - 1) // Two spaces for perfect alignment document.write(" " + " "); else { document.write(" " + middle); middle--; } } // Condition for the right Values if (row < N) { --right; document.write(" " + right); } else { ++right; document.write(" " + right); } document.write("<br>"); } } // Driver Code // Size of the Pattern var N = 4; alphabetPattern(N); </script>
Producción:
1 4 2 3 3 2 4 3 2 1 3 2 2 3 1 4
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