Dado un número n y usando números 0-n, debe imprimir dicho patrón.
Ejemplos:
Input : n = 5 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 5 4 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0 Input : n = 3 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0
La idea aquí es contar el espacio al principio de la string. En este patrón hay ((2 * n) + 1) filas. En las filas de 0 a n el número de espacios es (2 * (n – i)). En el número de fila de (n + 1) a (2 * n), el número de espacios es ((i – n) * 2).
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code to print the pattern #include <bits/stdc++.h> using namespace std; // function to generate the pattern. void pattern(int n) { // putting the space in line 1 for (int i = 1; i <= n * 2; i++) cout << " "; cout << 0 << endl; // generating the middle pattern. for (int i = 1; i <= (n * 2) - 1; i++) { // printing the increasing pattern if (i < n) { for (int j = 1; j <= (n - i) * 2; j++) cout << " "; } else { for (int j = 1; j <= (i % n) * 2; j++) cout << " "; } if (i < n) { for (int j = 0; j <= i % n; j++) cout << j << " "; for (int j = (i % n) - 1; j > 0; j--) cout << j << " "; cout << 0; } // printing the decreasing pattern else if (i > n) { for (int j = 0; j <= n - (i - n); j++) cout << j << " "; for (int j = (n - (i - n)) - 1; j > 0; j--) cout << j << " "; cout << 0; } else { for (int j = 0; j <= n; j++) cout << j << " "; for (int j = n - 1; j > 0; j--) cout << j << " "; cout << 0; } cout << endl; } // putting the space in last line for (int i = 1; i <= n * 2; i++) cout << " "; cout << 0; } // driver function. int main() { int n = 4; pattern(n); return 0; }
Java
// Java code to print the pattern import java.util.*; import java.lang.*; public class GeeksforGeeks{ // function to generate the pattern. public static void pattern(int n){ // putting the space in line 1 for (int i = 1; i <= n * 2; i++) System.out.print(" "); System.out.print(0 + "\n"); // generating the middle pattern. for (int i = 1; i <= (n * 2) - 1; i++) { // printing the increasing pattern if (i < n) { for (int j = 1; j <= (n - i) * 2; j++) System.out.print(" "); } else { for (int j = 1; j <= (i % n) * 2; j++) System.out.print(" "); } if (i < n) { for (int j = 0; j <= i % n; j++) System.out.print(j + " "); for (int j = (i % n) - 1; j > 0; j--) System.out.print(j + " "); System.out.print(0); } // printing the decreasing pattern else if (i > n) { for (int j = 0; j <= n - (i - n); j++) System.out.print(j + " "); for (int j = (n - (i - n)) - 1; j > 0; j--) System.out.print(j + " "); System.out.print(0); } else { for (int j = 0; j <= n; j++) System.out.print(j + " "); for (int j = n - 1; j > 0; j--) System.out.print(j + " "); System.out.print(0); } System.out.print("\n"); } // putting the space in last line for (int i = 1; i <= n * 2; i++) System.out.print(" "); System.out.print(0); } // driver code public static void main(String argc[]){ int n = 4; pattern(n); } } /*This code is contributed by Sagar Shukla.*/
Python3
# Python3 code to print the pattern # function to generate the pattern. def pattern(n): # putting the space in line 1 for i in range(1, n * 2 + 1): print(end = " ") print("0") # generating the middle pattern. for i in range(1, n * 2): # printing the increasing pattern if (i < n): for j in range(1, (n - i) * 2 + 1): print(end = " ") else: for j in range(1, (i % n) * 2 + 1): print(end = " ") if (i < n): for j in range(i % n + 1): print(j, end = " ") for j in range(i % n - 1, -1, -1): print(j, end = " ") # printing the decreasing pattern elif (i > n): for j in range(n - (i - n) + 1): print(j, end = " ") for j in range((n - (i - n)) - 1, -1, -1): print(j, end = " ") else: for j in range(n + 1): print(j, end = " ") for j in range(n - 1, -1, -1): print(j, end = " ") print() # putting the space in last line for i in range(1, n * 2 + 1): print(end = " ") print("0", end = "") # Driver Code n = 4; pattern(n); # This code is contributed by # mohit kumar 29
C#
// C# code to print the pattern using System; public class GeeksforGeeks{ // function to generate the pattern. public static void pattern(int n){ // putting the space in line 1 for (int i = 1; i <= n * 2; i++) Console.Write(" "); Console.Write(0 + "\n"); // generating the middle pattern. for (int i = 1; i <= (n * 2) - 1; i++) { // printing the increasing pattern if (i < n) { for (int j = 1; j <= (n - i) * 2; j++) Console.Write(" "); } else { for (int j = 1; j <= (i % n) * 2; j++) Console.Write(" "); } if (i < n) { for (int j = 0; j <= i % n; j++) Console.Write(j + " "); for (int j = (i % n) - 1; j > 0; j--) Console.Write(j + " "); Console.Write(0); } // printing the decreasing pattern else if (i > n) { for (int j = 0; j <= n - (i - n); j++) Console.Write(j + " "); for (int j = (n - (i - n)) - 1; j > 0; j--) Console.Write(j + " "); Console.Write(0); } else { for (int j = 0; j <= n; j++) Console.Write(j + " "); for (int j = n - 1; j > 0; j--) Console.Write(j + " "); Console.Write(0); } Console.Write("\n"); } // putting the space in last line for (int i = 1; i <= n * 2; i++) Console.Write(" "); Console.Write(0); } // driver code public static void Main(string []argc){ int n = 4; pattern(n); } } // This code is contributed by rutvik_56.
PHP
<?php // PHP code to print // the pattern // function to generate // the pattern. function pattern($n) { // putting the // space in line 1 for ($i = 1; $i <= $n * 2; $i++) echo " "; echo 0 , "\n"; // generating the // middle pattern. for ($i = 1; $i <= ($n * 2) - 1; $i++) { // printing the // increasing pattern if ($i < $n) { for ($j = 1; $j <= ($n - $i) * 2; $j++) echo " "; } else { for ($j = 1; $j <= ($i % $n) * 2; $j++) echo " "; } if ($i < $n) { for ($j = 0; $j <= $i % $n; $j++) echo $j , " "; for ($j = ($i % $n) - 1; $j > 0; $j--) echo $j , " "; echo 0; } // printing the // decreasing pattern else if ($i > $n) { for ($j = 0; $j <= $n - ($i - $n); $j++) echo $j , " "; for ($j = ($n - ($i - $n)) - 1; $j > 0; $j--) echo $j , " "; echo 0; } else { for ($j = 0; $j <= $n; $j++) echo $j ," "; for ($j = $n - 1; $j > 0; $j--) echo $j , " "; echo 0; } echo "\n"; } // putting the space // in last line for ($i = 1; $i <= $n * 2; $i++) echo " "; echo 0; } // Driver Code $n = 4; pattern($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript code to print the pattern // function to generate the pattern. function pattern(n) { // putting the space in line 1 for (let i = 1; i <= n * 2; i++) document.write(" "); document.write(0 + "<br>"); // generating the middle pattern. for (let i = 1; i <= (n * 2) - 1; i++) { // printing the increasing pattern if (i < n) { for (let j = 1; j <= (n - i) * 2; j++) document.write(" "); } else { for (let j = 1; j <= (i % n) * 2; j++) document.write(" "); } if (i < n) { for (let j = 0; j <= i % n; j++) document.write(j + " "); for (let j = (i % n) - 1; j > 0; j--) document.write(j + " "); document.write(0); } // printing the decreasing pattern else if (i > n) { for (let j = 0; j <= n - (i - n); j++) document.write(j + " "); for (let j = (n - (i - n)) - 1; j > 0; j--) document.write(j + " "); document.write(0); } else { for (let j = 0; j <= n; j++) document.write(j + " "); for (let j = n - 1; j > 0; j--) document.write(j + " "); document.write(0); } document.write("<br>"); } // putting the space in last line for (let i = 1; i <= n * 2; i++) document.write(" "); document.write(0); } let n = 4; pattern(n); // This code is contributed by patel2127 </script>
Producción:
0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0
Complejidad del tiempo : O(n 2 )
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por Abhishek Sharma 44 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA