En este artículo, dado el valor de n (longitud del alfabeto) y k (ancho del alfabeto), aprenderemos cómo imprimir el patrón «GFG» usando estrellas y espacios en blanco.
Ejemplos:
INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OUTPUT: ******* ******* ******* * * * * * * * * * * * * * ***** ******* * ***** * * * * * * * * * * * * * * * * * * * * ******* * *******
C++
#include <iostream> using namespace std; // Function to print the pattern "GFG" void print1(int n, int k) { int i, j; for (i = 0; i < n; i++) { cout << "\n"; for (j = 0; j < (3 * k + 2); j++) { if ((i == 0 && j != k && /*For printing the upper portion of the pattern "GFG"*/ j != 2 * k + 1) || ((i == n / 2) && (j > 1) && (j != k) && (j != 2 * k + 1) && /* for printing the middle portion of the pattern "GFG" */ (j != 2 * k + 3)) || ((i == n - 1) && (j != k) && /* for printing the lower portion of the pattern "GFG" */ ((j <= k) || (j > 2 * k + 1))) || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) || ((j == k - 1 || j == 3 * k + 1) && (i > n / 2))) cout << "*"; // printing * wherever required else cout << " "; // printing space wherever required } } } // Driver code int main() { int n = 7; // the length of the pattern "GFG" int k = 5; // the width of the pattern "GFG" print1(n, k); }
Java
import java.util.Scanner; public class PatternGFG // create a Class named PatternGFG { // Function to print the pattern "GFG" private static void print(int n, int k) { for (int i = 0; i < n; i++) { System.out.println(); for (int j = 0; j < (3 * k + 2); j++) { // For printing the upper portion of // the pattern "GFG" if ((i == 0 && j != k && j != 2 * k + 1) || ((i == n / 2) && (j > 1) && (j != k) && // for printing the middle portion of // the pattern "GFG" (j != 2 * k + 1) && (j != 2 * k + 3)) || ((i == n - 1) && (j != k) && // for printing the lower portion of // the pattern "GFG" ((j <= k) || (j > 2 * k + 1))) || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) || ((j == k - 1 || j == 3 * k + 1) && (i > n / 2))) // printing * wherever required System.out.print("*"); else System.out.print(" "); // printing space wherever required } } } // Driver code public static void main(String[] args) { int n = 7, k = 5; // length and width of the pattern print(n, k); } }
Python3
# Python Program to print # the pattern “GFG” import math # Function to print the # pattern "GFG" def print1(n, k) : for i in range(0, n) : print ("\n") for j in range(0, (3 * k + 2)) : if ((i == 0 and j != k and # For printing the # upper portion of # the pattern "GFG" j != 2 * k + 1) or ((i == math.floor(n / 2)) and (j > 1) and (j != k) and (j != 2 * k + 1) and # for printing the # middle portion of # the pattern "GFG" (j != 2 * k + 3)) or ((i == n - 1) and (j != k) and # for printing the # lower portion of # the pattern "GFG" ((j <= k) or (j > 2 * k + 1))) or (j == 0) or (j == k + 1) or (j == (2 * k + 2)) or ((j ==k - 1 or j == 3 * k + 1) and (i > math.floor(n / 2)))) : # printing * where # ever required print ("*", end = "") else : # printing space # wherever required print (" ", end = "") # Driver code # the length of the # pattern "GFG" n = 7 # the width of the # pattern "GFG" k = 5 print1(n, k) # This code is contributed # by Manish Shaw(manishshaw1)
C#
// C# code for printing pattern. using System; public class GFG { // Function to print the pattern "GFG" private static void print(int n, int k) { for (int i = 0; i < n; i++) { Console.WriteLine(); for (int j = 0; j < (3 * k + 2); j++) { // For printing the upper portion of // the pattern "GFG" if ((i == 0 && j != k && j != 2 * k + 1) || ((i == n / 2) && (j > 1) && (j != k) && // for printing the middle portion of // the pattern "GFG" (j != 2 * k + 1) && (j != 2 * k + 3)) || ((i == n - 1) && (j != k) && // for printing the lower portion of // the pattern "GFG" ((j <= k) || (j > 2 * k + 1))) || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) || ((j == k - 1 || j == 3 * k + 1) && (i > n / 2))) // printing * wherever required Console.Write("*"); else Console.Write(" "); } } } // Driver code public static void Main() { // length and width of the pattern int n = 7, k = 5; print(n, k); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to print // the pattern “GFG” // Function to print the // pattern "GFG" function print1($n, $k) { for ($i = 0; $i < $n; $i++) { echo "\n"; for ($j = 0; $j < (3 * $k + 2); $j++) { if (($i == 0 && $j != $k && // For printing the upper portion // of the pattern "GFG" $j != 2 * $k + 1) || (($i == floor($n / 2)) && ($j > 1) && ($j != $k) && ($j != 2 * $k + 1) && /* for printing the middle portion of the pattern "GFG" */ ($j != 2 * $k + 3)) || (($i == $n - 1) && ($j != $k) && /* for printing the lower portion of the pattern "GFG" */ (($j <= $k) || ($j > 2 * $k + 1))) || ($j == 0) || ($j == $k + 1) || ($j == (2 * $k + 2)) || (($j ==$k - 1 || $j == 3 * $k + 1) && ($i > floor($n / 2)))) // printing * wherever required echo "*"; else // printing space wherever required echo " "; } } } // Driver code // the length of the pattern "GFG" $n = 7; // the width of the pattern "GFG" $k = 5; print1($n, $k); // This code is contributed by Sam007 ?>
Javascript
<script> // Javascript implementation for the above approach // Function to print the pattern "GFG" function print1(n, k) { var i, j; for (i = 0; i < n; i++) { document.write("<br>"); for (j = 0; j < (3 * k + 2); j++) { if ((i == 0 && j != k && /*For printing the upper portion of the pattern "GFG"*/ j != 2 * k + 1) || ((i == n / 2) && (j > 1) && (j != k) && (j != 2 * k + 1) && /* for printing the middle portion of the pattern "GFG" */ (j != 2 * k + 3)) || ((i == n - 1) && (j != k) && /* for printing the lower portion of the pattern "GFG" */ ((j <= k) || (j > 2 * k + 1))) || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) || ((j == k - 1 || j == 3 * k + 1) && (i > n / 2))) document.write("*"); // printing * wherever required else document.write(" "," "); // printing space wherever required } } } // Driver code var n = 7; // the length of the pattern "GFG" var k = 5; // the width of the pattern "GFG" print1(n, k); // This code is contributed by Shubham Singh </script>
Producción :
***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * *****
Complejidad de tiempo: O(n * k), donde n y k representan las entradas dadas.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Publicación traducida automáticamente
Artículo escrito por Harshita Pandey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA