Imprima el patrón en el que hay un cuadrado hueco y un signo más dentro. El patrón será según el n, es decir, el número de filas dado como se muestra en el ejemplo.
Ejemplos:
Input : 6 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Acercarse:
- Comenzaremos un bucle for hasta n y dentro de este también hay un bucle for hasta n.
- En esto simplemente tenemos que verificar si la fila es la primera o la última o la columna es la primera o la última, luego imprima «*».
- Ahora tenemos que verificar la fila y la columna del medio.
- Entonces, cuando n es impar, tendremos una fila y una columna en el medio y si la fila o la columna están en el medio, imprimiremos «*».
- Si n es par, entonces filas o columnas si son iguales a estos valores n/2 y (n/2)+1, entonces imprimiremos “*”.
- De lo contrario, en todas partes tenemos que imprimir ” “(espacio).
A continuación se muestra la implementación.
C++14
// C++ program to print the pattern // hollow square with plus inside it // window pattern #include <bits/stdc++.h> using namespace std; // Function to print pattern n means // number of rows which we want void window_pattern (int n) { int c, d; // If n is odd then we will have // only one middle element if (n % 2 != 0) { c = (n / 2) + 1; d = 0; } // If n is even then we will have two // values else { c = (n / 2) + 1; d = n / 2 ; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { // If i,j equals to corner row or // column then "*" if (i == 1 || j == 1 || i == n || j == n) cout << "* "; else { // If i,j equals to the middle // row or column then "*" if (i == c || j == c) cout << "* "; else if (i == d || j == d) cout << "* "; else cout << " "; } } cout << '\n'; } } // Driver Code int main() { int n = 7; window_pattern(n); return 0; } // This code is contributed by himanshu77
Java
// Java program to print the pattern // hollow square with plus inside it // window pattern class GFG { // Function to print pattern n means // number of rows which we want static void window_pattern (int n) { int c, d; // If n is odd then we will have // only one middle element if (n % 2 != 0) { c = (n / 2) + 1; d = 0; } // If n is even then we will have // two values else { c = (n / 2) + 1; d = n / 2 ; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { // If i,j equals to corner row // or column then "*" if (i == 1 || j == 1 || i == n || j == n) System.out.print("* "); else { // If i,j equals to the middle // row or column then "*" if (i == c || j == c) System.out.print("* "); else if (i == d || j == d) System.out.print("* "); else System.out.print(" "); } } System.out.println(); } } // Driver code public static void main(String[] args) { int n = 7; window_pattern(n); } } // This code is contributed by divyeshrabadiya07
Python3
# Python3 program to print the pattern # hollow square with plus inside it # window pattern # function to print pattern n means # number of rows which we want def window_pattern(n): # if n is odd then we will have # only one middle element if n % 2 != 0: c = ( n // 2 ) + 1 d = 0 # if n is even then we will have two # values else: c = ( n // 2 ) + 1 d = ( n // 2 ) for i in range( 1 , n + 1 ): for j in range( 1 , n + 1 ): # if i,j equals to corner row or # column then "*" if i == 1 or j == 1 or i == n or j == n: print("*",end=" ") else: # if i,j equals to the middle row # or column then "*" if i == c or j == c: print("*",end=" ") elif i == d or j == d: print("*",end=" ") else: print(" ",end=" ") print() # Driver Code if __name__ == "__main__": n = 7 window_pattern(n)
C#
// C# program to print the pattern // hollow square with plus inside it // window pattern using System; class GFG{ // Function to print pattern n means // number of rows which we want static void window_pattern (int n) { int c, d; // If n is odd then we will have // only one middle element if (n % 2 != 0) { c = (n / 2) + 1; d = 0; } // If n is even then we will have // two values else { c = (n / 2) + 1; d = n / 2 ; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { // If i,j equals to corner row // or column then "*" if (i == 1 || j == 1 || i == n || j == n) Console.Write("* "); else { // If i,j equals to the middle // row or column then "*" if (i == c || j == c) Console.Write("* "); else if (i == d || j == d) Console.Write("* "); else Console.Write(" "); } } Console.WriteLine(); } } // Driver code static void Main() { int n = 7; window_pattern(n); } } // This code is contributed by divyesh072019
Javascript
<script> // JavaScript program to // print the pattern // hollow square with // plus inside it // window pattern // Function to print pattern n means // number of rows which we want function window_pattern(n) { var c, d; // If n is odd then we will have // only one middle element if (n % 2 != 0) { c = parseInt(n / 2 + 1); d = 0; } // If n is even then we will have two // values else { c = parseInt(n / 2 + 1); d = parseInt(n / 2); } for (var i = 1; i <= n; i++) { for (var j = 1; j <= n; j++) { // If i,j equals to corner row or // column then "*" if (i == 1 || j == 1 || i == n || j == n) document.write("* "); else { // If i,j equals to the middle // row or column then "*" if (i == c || j == c) document.write("* "); else if (i == d || j == d) document.write("* "); else document.write(" "); } } document.write("<br>"); } } // Driver Code var n = 7; window_pattern(n); </script>
Producción :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Complejidad temporal: O(n 2 )
Publicación traducida automáticamente
Artículo escrito por kashishmishra9911 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA