Dado un número entero N que es el número de filas , la tarea es dibujar el patrón numérico en forma de flecha de dos puntas .
Requisito previo: el patrón es un patrón de tipo de crecimiento y reducción y, por lo tanto, se requieren conocimientos básicos para ejecutar bucles para comprender el tema y el código en cualquier idioma. La forma geométrica se puede visualizar como-
Ejemplos:
Input: R = 7 Output: 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 Input: R = 9 Output: 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
Acercarse:
- En el ejemplo dado, N=7 y el número de FILAS es 7.
- VERTICALMENTE, el patrón CRECE hasta FILA=N/2 y luego SE ENCOGE.
- FILA 1 tiene 4 ” “(ESPACIO) caracteres y luego un valor.
- El número de caracteres de ESPACIO disminuye mientras que los NÚMEROS aumentan en el conteo en cada fila sucesiva.
- Además, tenga en cuenta que el primer valor del número colocado en cada fila es el mismo que el número de la fila.
- También HORIZONTALMENTE el patrón tiene NÚMEROS, luego ESPACIOS y luego NÚMEROS.
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 required pattern void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { cout << " " << " "; csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { cout << val1 << " "; val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { cout << " " << " "; csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { cout << val2 << " "; val2 = val2 + 1; cst2 = cst2 + 1; } } cout << endl; // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code int main() { // Number of rows int N = 7; drawPattern(N); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the required pattern static void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { System.out.print(" "); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { System.out.print(val1 + " "); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { System.out.print(" "); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { System.out.print(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } System.out.println(); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code public static void main(String args[]) { // Number of rows int N = 7; drawPattern(N); } }
Python3
# Python3 implementation of the approach # Function to print the required pattern def drawPattern(N) : n = N; row = 1; # 'nst' is the number of values nst = 1; # 'nsp' is the number of spaces nsp1 = n - 1; nsp2 = -1; val1 = row; val2 = 1; while (row <= n) : # Here spaces are printed # 'csp' is the count of spaces csp1 = 1; while (csp1 <= nsp1) : print(" ",end= " "); csp1 = csp1 + 1; # Now, values are printed # 'cst' is the count of stars cst1 = 1; while (cst1 <= nst) : print(val1,end = " "); val1 = val1 - 1; cst1 = cst1 + 1; # Again spaces have to be printed csp2 = 1; while (csp2 <= nsp2) : print(" ",end = " "); csp2 = csp2 + 1; # Again values have to be printed if (row != 1 and row != n) : cst2 = 1; while (cst2 <= nst) : print(val2,end = " "); val2 = val2 + 1; cst2 = cst2 + 1; print() # Move to the next row if (row <= n // 2) : nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; else : nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; row = row + 1; # Driver code if __name__ == "__main__" : # Number of rows N = 7; drawPattern(N); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to print the required pattern static void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { Console.Write(" "); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { Console.Write(val1 + " "); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { Console.Write(" "); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { Console.Write(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } Console.WriteLine(); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code public static void Main() { // Number of rows int N = 7; drawPattern(N); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript implementation of the approach // Function to print the required pattern function drawPattern(N) { var n = N; var row = 1; // 'nst' is the number of values var nst = 1; // 'nsp' is the number of spaces var nsp1 = n - 1; var nsp2 = -1; var val1 = row; var val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces var csp1 = 1; while (csp1 <= nsp1) { document.write(" " + " "); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars var cst1 = 1; while (cst1 <= nst) { document.write(val1 + " "); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed var csp2 = 1; while (csp2 <= nsp2) { document.write(" " + " "); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { var cst2 = 1; while (cst2 <= nst) { document.write(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } document.write("<br>"); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code // Number of rows var N = 7; drawPattern(N); </script>
Producción:
1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
Complejidad de tiempo: O(N 2 )
Complejidad de espacio: O(1)
Publicación traducida automáticamente
Artículo escrito por medha130101 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA