Dados 3 enteros positivos c1, c2 y n, donde n es el tamaño de la array cuadrada 2-D. La tarea es imprimir la array rellena con un patrón rectangular que tenga las coordenadas centrales c1, c2 de modo que 0 <= c1, c2 < n.
Ejemplos:
Entrada : c1 = 2, c2 = 2, n = 5
Salida :
2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2Entrada: c1 = 3, c2 = 4, n = 7
Salida:
4 3 3 3 3 3 3
4 3 2 2 2 2 2
4 3 2 1 1 1 2
4 3 2 1 0 1 2
4 3 2 1 1 1 2
4 3 2 2 2 2 2
4 3 3 3 3 3 3
Enfoque: este problema se puede resolver utilizando dos bucles anidados. Siga los pasos a continuación para resolver este problema:
- Iterar en el rango[0, N-1], usando una variable i y seguir los siguientes pasos:
- Iterar en el rango[0, N-1], usando una variable j y seguir los siguientes pasos:
- Imprime el máximo de abs(c1 – i) y abs(c2 – j).
- Imprimir nueva línea.
- Iterar en el rango[0, N-1], usando una variable j y seguir los siguientes pasos:
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the matrix filled // with rectangle pattern having center // coordinates are c1, c2 void printRectPattern(int c1, int c2, int n) { // Iterate in the range[0, n-1] for (int i = 0; i < n; i++) { // Iterate in the range[0, n-1] for (int j = 0; j < n; j++) { cout << (max(abs(c1 - i), abs(c2 - j))) << " "; } cout << endl; } } // Driver Code int main() { // Given Input int c1 = 2; int c2 = 2; int n = 5; // Function Call printRectPattern(c1, c2, n); // This code is contributed by Potta Lokesh return 0; }
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to print the matrix filled // with rectangle pattern having center // coordinates are c1, c2 static void printRectPattern(int c1, int c2, int n) { // Iterate in the range[0, n-1] for(int i = 0; i < n; i++) { // Iterate in the range[0, n-1] for(int j = 0; j < n; j++) { System.out.print((Math.max(Math.abs(c1 - i), Math.abs(c2 - j))) + " "); } System.out.println(); } } // Driver code public static void main(String[] args) { // Given Input int c1 = 2; int c2 = 2; int n = 5; // Function Call printRectPattern(c1, c2, n); } } // This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach # Function to print the matrix filled # with rectangle pattern having center # coordinates are c1, c2 def printRectPattern(c1, c2, n): # Iterate in the range[0, n-1] for i in range(n): # Iterate in the range[0, n-1] for j in range(n): print(max(abs(c1 - i), abs(c2 - j)), end = " ") print("") # Driver Code # Given Input c1 = 2 c2 = 2 n = 5 # Function Call printRectPattern(c1, c2, n)
C#
// C# program for the above approach using System; class GFG{ // Function to print the matrix filled // with rectangle pattern having center // coordinates are c1, c2 static void printRectPattern(int c1, int c2, int n) { // Iterate in the range[0, n-1] for(int i = 0; i < n; i++) { // Iterate in the range[0, n-1] for(int j = 0; j < n; j++) { Console.Write((Math.Max(Math.Abs(c1 - i), Math.Abs(c2 - j))) + " "); } Console.WriteLine(); } } // Driver Code public static void Main(String[] args) { // Given Input int c1 = 2; int c2 = 2; int n = 5; // Function Call printRectPattern(c1, c2, n); } } // This code is contributed by target_2
Javascript
<script> // Javascript program for the above approach // Function to print the matrix filled // with rectangle pattern having center // coordinates are c1, c2 function printRectPattern(c1, c2, n) { // Iterate in the range[0, n-1] for (let i = 0; i < n; i++) { // Iterate in the range[0, n-1] for (let j = 0; j < n; j++) { document.write(Math.max(Math.abs(c1 - i), Math.abs(c2 - j)) + " "); } document.write("<br>"); } } // Driver Code // Given Input let c1 = 2; let c2 = 2; let n = 5; // Function Call printRectPattern(c1, c2, n); // This code is contributed by gfgking </script>
2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 2 2
Complejidad de tiempo: O(N ^2)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pradiptamukherjee y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA