Para un valor N dado, que denota el número de Charters a partir de A, imprima el patrón de puente de caracteres inverso.
Ejemplos:
Input : n = 5 Output : ABCDEDCBA ABCD DCBA ABC CBA AB BA A A Input : n = 8 Output : ABCDEFGHGFEDCBA ABCDEFG GFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
- Para un valor N dado, refleja el número de caracteres que participan en el patrón, comenzando desde A. Para N = 5, el carácter participante sería ABCD E.
- Al usar un bucle for anidado, calcularíamos la lógica. Donde el bucle exterior de ‘i’ oscilaría entre 0 y N y el bucle interior de ‘j’ oscilaría entre 65 (Inicio) y 64 + 2*N.
- Bajo el cual verificaríamos la condición requerida para el diseño del patrón. Para todos los valores de j que son menores que ((64+n)+ i) imprimiría el (char)((64 + n)-( j % (64+n))) y para todos los valores de j <= ((64+n) -i) imprimiría (char)j.
C++
// CPP program to print reverse character bridge pattern #include <iostream> using namespace std; // Function to print pattern void ReverseCharBridge(int n) { for (int i = 0; i < n; i++) { for (int j = 'A'; j < 'A' + (2 * n) - 1; j++) { if (j >= ('A' + n - 1) + i) cout << (char)(('A' + n - 1) - (j % ('A' + n - 1))); else if (j <= ('A' + n - 1) - i) cout << (char)j; else cout << " "; } cout << endl; } } // Driver Code int main() { int n = 6; ReverseCharBridge(n); return 0; }
Java
// Java program to print reverse // character bridge pattern import java.io.*; class GFG { // Function to print pattern static void ReverseCharBridge(int n) { for (int i = 0; i < n; i++) { for (int j = 'A'; j < 'A' + (2 * n) - 1; j++) { if (j >= ('A' + n - 1) + i) System.out.print((char)(('A' + n - 1) - (j % ('A' + n - 1)))); else if (j <= ('A' + n - 1) - i) System.out.print((char)j); else System.out.print(" "); } System.out.println(); } } // Driver Code public static void main(String args[]) { int n = 6; ReverseCharBridge(n); } } /*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 code to print reverse # character bridge pattern # Function to print pattern def ReverseCharBridge( n ): for i in range( n ): for j in range( ord('A'), ord('A') + (2 * n) - 1): if j >= (ord( 'A' ) + n - 1) + i: print(chr((ord('A') + n - 1) - (j % (ord('A') + n - 1))), end = '') elif j <= (ord('A') + n - 1) - i: print(chr(j), end = '') else: print(end = " ") print("\n", end = '') # Driver Code n = 6 ReverseCharBridge(n) # This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to print reverse // character bridge pattern using System; class GFG { // Function to print pattern static void ReverseCharBridge(int n) { for (int i = 0; i < n; i++) { for (int j = 'A'; j < 'A' + (2 * n) - 1; j++) { if (j >= ('A' + n - 1) + i) Console.Write((char)(('A' + n - 1) - (j % ('A' + n - 1)))); else if (j <= ('A' + n - 1) - i) Console.Write((char)j); else Console.Write(" "); } Console.WriteLine(); } } // Driver Code public static void Main() { int n = 6; ReverseCharBridge(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to print reverse // character bridge pattern // Function to print pattern function ReverseCharBridge($n) { //Ascii of A is 65 for ($i = 0; $i < $n; $i++) { for ($j = 65; $j < 65 + (2 * $n) - 1; $j++) { if ($j >= (65 + $n - 1) + $i) echo chr((65 + $n - 1) - ($j % (65 + $n - 1))); else if ($j <= (65 + $n - 1) - $i) echo chr($j); else echo " "; } echo "\n"; } } // Driver Code $n = 6; ReverseCharBridge($n); // This code is contributed by mits ?>
Javascript
<script> // Javascript program to print reverse character bridge pattern // Function to print pattern function ReverseCharBridge(n) { for (let i = 0; i < n; i++) { for (let j = 65; j < 65 + (2 * n) - 1; j++) { if (j >= (65 + n - 1) + i) document.write(String.fromCharCode((65 + n - 1) - (j % (65 + n - 1)))); else if (j <= (65 + n - 1) - i) document.write(String.fromCharCode(j)); else document.write(" "); } document.write("\n"); } } // Driver Code let n = 6; ReverseCharBridge(n); // This code is contributed by Samim Hossain Mondal. </script>
Producción
ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
Complejidad temporal: O(n 2 )
Espacio Auxiliar: O(1)