Dado un número entero N , la tarea es imprimir el siguiente patrón para el valor dado de N .
Para N = 5, a continuación se muestra el patrón dado:
Ejemplos:
Entrada: N = 4
Salida:
1020304017018019020
**50607014015016
****809012013
******10011Entrada: N = 3
Salida:
10203010011012
**4050809
****607
Enfoque: La idea para comprender la lógica detrás del patrón dado se establece a continuación:
Al mirar de cerca, vemos que al reemplazar los ceros intermedios con espacios, el patrón se puede ver más claramente. El patrón se divide además en tres patrones diferentes.
- Caso 1: el patrón de caracteres de asterisco (*) sigue una secuencia desde 0 y agrega dos asteriscos más en cada fila, donde la fila es igual a N.
- Caso 2: En esta parte, el patrón es muy sencillo de entender. es decir, el número de columnas y filas será igual a N y seguirá una secuencia como 1, 2, 3, 4, 5…
- Caso 3: La secuencia de seguimiento o de abajo hacia arriba es la parte interesante donde los números se representan de abajo hacia arriba .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to print // the given pattern #include <bits/stdc++.h> using namespace std; // Function to find the sum of // N integers from 1 to N int sum(int n) { return n * (n - 1) / 2; } // Function to print the given // pattern void BSpattern(int N) { int Val = 0, Pthree = 0, cnt = 0, initial; string s = "**"; // Iterate over [0, N - 1] for (int i = 0; i < N; i++) { cnt = 0; // Sub-Pattern - 1 if (i > 0) { cout << s; s += "**"; } // Sub-Pattern - 2 for (int j = i; j < N; j++) { // Count the number of element // in rows and sub-pattern 2 and 3 // will have same rows if (i > 0) { cnt++; } // Increment Val to print the // series 1, 2, 3, 4, 5 ... cout << ++Val; cout << 0; } // To get the first element of sub // pattern 3 find the sum of first N-1 // elements first N-1 elements in row1 // previous of Sub-Pattern 2 // Finally, add the (N-1)th element // i.e., 5 and increment it by 1 if (i == 0) { int Sumbeforelast = sum(Val) * 2; Pthree = Val + Sumbeforelast + 1; initial = Pthree; } // Initial is used to give the initial // value of the row in Sub-Pattern 3 initial = initial - cnt; Pthree = initial; // Sub-Pattern 3 for (int k = i; k < N; k++) { cout << Pthree++; // Skip printing zero at the last if (k != N - 1) { cout << 0; } } cout << "\n"; } } // Driver Code int main() { // Given N int N = 5; // Function Call BSpattern(N); return 0; }
Java
// Java implementation to print // the given pattern import java.util.*; class GFG{ // Function to find the sum of // N integers from 1 to N static int sum(int n) { return n * (n - 1) / 2; } // Function to print the given // pattern static void BSpattern(int N) { int Val = 0, Pthree = 0, cnt = 0, initial = -1; String s = "**"; // Iterate over [0, N - 1] for(int i = 0; i < N; i++) { cnt = 0; // Sub-Pattern - 1 if (i > 0) { System.out.print(s); s += "**"; } // Sub-Pattern - 2 for(int j = i; j < N; j++) { // Count the number of element // in rows and sub-pattern 2 // and 3 will have same rows if (i > 0) { cnt++; } // Increment Val to print the // series 1, 2, 3, 4, 5 ... System.out.print(++Val); System.out.print("0"); } // To get the first element of sub // pattern 3 find the sum of first N-1 // elements first N-1 elements in row1 // previous of Sub-Pattern 2 // Finally, add the (N-1)th element // i.e., 5 and increment it by 1 if (i == 0) { int Sumbeforelast = sum(Val) * 2; Pthree = Val + Sumbeforelast + 1; initial = Pthree; } // Initial is used to give the initial // value of the row in Sub-Pattern 3 initial = initial - cnt; Pthree = initial; // Sub-Pattern 3 for(int k = i; k < N; k++) { System.out.print(Pthree++); // Skip printing zero at the last if (k != N - 1) { System.out.print("0"); } } System.out.println(); } } // Driver code public static void main(String[] args) { // Given N int N = 5; // Function call BSpattern(N); } } // This code is contributed by offbeat
Python3
# Python3 implementation to print # the given pattern # Function to find the sum of # N integers from 1 to N def sum(n): return n * (n - 1) // 2 # Function to print the given # pattern def BSpattern(N): Val = 0 Pthree = 0, cnt = 0 initial = -1 s = "**" # Iterate over [0, N - 1] for i in range(N): cnt = 0 # Sub-Pattern - 1 if (i > 0): print(s, end = "") s += "**" # Sub-Pattern - 2 for j in range(i, N): # Count the number of element # in rows and sub-pattern 2 and 3 # will have same rows if (i > 0): cnt += 1 # Increment Val to print the # series 1, 2, 3, 4, 5 ... Val += 1 print(Val, end = "") print(0, end = "") # To get the first element of sub # pattern 3 find the sum of first N-1 # elements first N-1 elements in row1 # previous of Sub-Pattern 2 # Finally, add the (N-1)th element # i.e., 5 and increment it by 1 if (i == 0): Sumbeforelast = sum(Val) * 2 Pthree = Val + Sumbeforelast + 1 initial = Pthree # Initial is used to give the initial # value of the row in Sub-Pattern 3 initial = initial - cnt Pthree = initial # Sub-Pattern 3 for k in range(i, N): print(Pthree, end = "") Pthree += 1 # Skip printing zero at the last if (k != N - 1): print(0, end = "") print() # Driver Code # Given N N = 5 # Function call BSpattern(N) # This code is contributed by sanjoy_62
C#
// C# implementation to print // the given pattern using System; class GFG{ // Function to find the sum of // N integers from 1 to N static int sum(int n) { return n * (n - 1) / 2; } // Function to print the given // pattern static void BSpattern(int N) { int Val = 0, Pthree = 0, cnt = 0, initial = -1; String s = "**"; // Iterate over [0, N - 1] for(int i = 0; i < N; i++) { cnt = 0; // Sub-Pattern - 1 if (i > 0) { Console.Write(s); s += "**"; } // Sub-Pattern - 2 for(int j = i; j < N; j++) { // Count the number of element // in rows and sub-pattern 2 // and 3 will have same rows if (i > 0) { cnt++; } // Increment Val to print the // series 1, 2, 3, 4, 5 ... Console.Write(++Val); Console.Write("0"); } // To get the first element of sub // pattern 3 find the sum of first N-1 // elements first N-1 elements in row1 // previous of Sub-Pattern 2 // Finally, add the (N-1)th element // i.e., 5 and increment it by 1 if (i == 0) { int Sumbeforelast = sum(Val) * 2; Pthree = Val + Sumbeforelast + 1; initial = Pthree; } // Initial is used to give the initial // value of the row in Sub-Pattern 3 initial = initial - cnt; Pthree = initial; // Sub-Pattern 3 for(int k = i; k < N; k++) { Console.Write(Pthree++); // Skip printing zero at the last if (k != N - 1) { Console.Write("0"); } } Console.WriteLine(); } } // Driver code public static void Main(String[] args) { // Given N int N = 5; // Function call BSpattern(N); } } // This code is contributed by shikhasingrajput
Javascript
<script> //Javascript implementation to print // the given pattern // Function to find the sum of // N integers from 1 to N function sum( n) { return n * parseInt((n - 1) / 2); } // Function to print the given // pattern function BSpattern( N) { var Val = 0, Pthree = 0, cnt = 0, initial; var s = "**"; // Iterate over [0, N - 1] for (var i = 0; i < N; i++) { cnt = 0; // Sub-Pattern - 1 if (i > 0) { document.write( s); s += "**"; } // Sub-Pattern - 2 for (var j = i; j < N; j++) { // Count the number of element // in rows and sub-pattern 2 and 3 // will have same rows if (i > 0) { cnt++; } // Increment Val to print the // series 1, 2, 3, 4, 5 ... document.write( ++Val); document.write( 0); } // To get the first element of sub // pattern 3 find the sum of first N-1 // elements first N-1 elements in row1 // previous of Sub-Pattern 2 // Finally, add the (N-1)th element // i.e., 5 and increment it by 1 if (i == 0) { var Sumbeforelast = sum(Val) * 2; Pthree = Val + Sumbeforelast + 1; initial = Pthree; } // Initial is used to give the initial // value of the row in Sub-Pattern 3 initial = initial - cnt; Pthree = initial; // Sub-Pattern 3 for (var k = i; k < N; k++) { document.write(Pthree++); // Skip printing zero at the last if (k != N - 1) { document.write( 0); } } document.write( "<br>"); } } // Given N var N = 5; // Function Call BSpattern(N); // This code is contributed by SoumikMondal </script>
102030405026027028029030 **6070809022023024025 ****10011012019020021 ******13014017018 ********15016
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por satvikshrivas26 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA