Escriba un programa que reciba un número como entrada y lo imprima en el siguiente formato como se muestra a continuación.
Ejemplos:
Input : n = 3 Output : 1*2*3*10*11*12 --4*5*8*9 ----6*7 Input : n = 4 Output : 1*2*3*4*17*18*19*20 --5*6*7*14*15*16 ----8*9*12*13 ------10*11
Preguntado en la ronda de codificación CGI
Enfoque: El enfoque es ver el problema, no como una sola tarea, sino como tres tareas que, al combinarse, completan la tarea principal. Las tres tareas son imprimir la mitad izquierda del patrón, imprimir guiones (-) e imprimir la mitad derecha del patrón. Combinando las tres tareas, podríamos imprimir el patrón.
left-half of pattern 1*2*3* --4*5* ----6* A function printdashes() to print the "-". right-half of pattern 10*11*12 *8*9 7
A continuación se muestra la implementación.
C++
// C program to print the given pattern #include <stdio.h> // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row void printdashes(int k) { int i; for (i = 1; i <= k; i++) printf("-"); } // function to print the pattern void pattern(int n){ // variables for vertical left half /* 1*2*3* --4*5* ----6* */ int row, column, dashes = 0; int i, j, dash_counter = 0; int value = 1; // variables for vertical right half /* 10*11*12 *8*9 7 */ int k, l, decrementor = 0; int column_decrementor = 0; int support = n - 1; int temp = ((n * n) + 1); int temp1 = (n * 2) - 1; int z = temp; int tracker; for (i = 1; i <= n; i++) { printdashes(dash_counter); // This part will take care of the vertical // left half of the pattern for (j = 1; j <= (2 * n) - dash_counter; j++) { // Printing the "*" in even positions if (j % 2 == 0) printf("*"); else { printf("%d", value); value++; } } // This part will take care of the vertical // right half of the pattern for (k = 1; k <= (temp1 - decrementor); k++) { // Printing the "*" in even positions if (k % 2 == 0) printf("*"); else { if (k == 1) tracker = temp; printf("%d", temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; printf("\n"); } } // driver program int main() { int n = 3; pattern(n); return 0; }
Java
// Java program to print the given pattern class GFG { // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row static void printdashes(int k) { int i; for (i = 1; i <= k; i++) System.out.print("-"); } // function to print the pattern static void pattern(int n) { // variables for vertical left half /* 1*2*3* --4*5* ----6* */ int row, column, dashes = 0; int i, j, dash_counter = 0; int value = 1; // variables for vertical right half /* 10*11*12 *8*9 7 */ int k, l, decrementor = 0; int column_decrementor = 0; int support = n - 1; int temp = ((n * n) + 1); int temp1 = (n * 2) - 1; int z = temp; int tracker = 0; for (i = 1; i <= n; i++) { printdashes(dash_counter); // This part will take care of the vertical // left half of the pattern for (j = 1; j <= (2 * n) - dash_counter; j++) { // Printing the "*" in even positions if (j % 2 == 0) System.out.print("*"); else { System.out.print(value); value++; } } // This part will take care of the vertical // right half of the pattern for (k = 1; k <= (temp1 - decrementor); k++) { // Printing the "*" in even positions if (k % 2 == 0) System.out.print("*"); else { if (k == 1) tracker = temp; System.out.print(temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; System.out.print("\n"); } } // Driver code public static void main(String arg[]) { int n = 3; pattern(n); } } // This code is contributed by Anant Agarwal.
Python3
# Python program to print # the given pattern # utility function to # print "-" in every # row. This will take # care of printing # "-" in the start of every row def printdashes(k): for i in range(1,k+1): print("-",end="") # function to print the pattern def pattern(n): # variables for vertical left half ''' 1*2*3* --4*5* ----6* ''' dashes = 0 dash_counter = 0 value = 1 # variables for vertical right half ''' 10*11*12 *8*9 7 ''' decrementor = 0 column_decrementor = 0 support = n - 1 temp = ((n * n) + 1) temp1 = (n * 2) - 1 z = temp for i in range(1,n+1): printdashes(dash_counter) # This part will take # care of the vertical # left half of the pattern for j in range(1,(((2 * n) - dash_counter)+1)): # Printing the "*" in even positions if (j % 2 == 0): print("*",end="") else: print(value,end="") value=value+1 # This part will take # care of the vertical # right half of the pattern for k in range(1,((temp1 - decrementor)+1)): # Printing the "*" in even positions if (k % 2 == 0): print("*",end="") else: if (k == 1): tracker = temp print(temp,end="") temp=temp + 1 decrementor =decrementor + 2 temp = tracker - support support=support - 1 # In every row, the number of dash counts # is increased by 2 dash_counter =dash_counter + 2 print("") # driver program n = 3 pattern(n) # This code is contributed # by Anant Agarwal.
C#
// C# program to print the given pattern using System; class GFG { // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row static void printdashes(int k) { int i; for (i = 1; i <= k; i++) Console.Write("-"); } // function to print the pattern static void pattern(int n) { // variables for vertical left half /* 1*2*3* --4*5* ----6* */ int i, j, dash_counter = 0; int value = 1; // variables for vertical right half /* 10*11*12 *8*9 7 */ int k, decrementor = 0; int support = n - 1; int temp = ((n * n) + 1); int temp1 = (n * 2) - 1; int tracker = 0; for (i = 1; i <= n; i++) { printdashes(dash_counter); // This part will take care of the vertical // left half of the pattern for (j = 1; j <= (2 * n) - dash_counter; j++) { // Printing the "*" in even positions if (j % 2 == 0) Console.Write("*"); else { Console.Write(value); value++; } } // This part will take care of the vertical // right half of the pattern for (k = 1; k <= (temp1 - decrementor); k++) { // Printing the "*" in even positions if (k % 2 == 0) Console.Write("*"); else { if (k == 1) tracker = temp; Console.Write(temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; Console.WriteLine(); } } // Driver code public static void Main() { int n = 3; pattern(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to print the given pattern // function to print the pattern function pattern($n) { // variables for vertical left half $dashes = 0; $dash_counter = 0; $value = 1; // variables for vertical right half $decrementor = 0; $column_decrementor = 0; $support = $n - 1; $temp = (($n * $n) + 1); $temp1 = ($n * 2) - 1; $z = $temp; for ($i = 1; $i <= $n; $i++) { // loop for printing dash for ($dd = 1; $dd <= $dash_counter; $dd++) printf("-"); // This part will take care of the // vertical left half of the pattern for ($j = 1; $j <= (2 * $n) - $dash_counter; $j++) { // Printing the "*" in even // positions if ($j % 2 == 0) printf("*"); else { printf($value); $value++; } } // This part will take care // of the vertical right half // of the pattern for ($k = 1; $k <= ($temp1 - $decrementor); $k++) { // Printing the "*" in even // positions if ($k % 2 == 0) printf("*"); else { if ($k == 1) $tracker = $temp; printf($temp); $temp++; } } $decrementor += 2; $temp = $tracker - $support; $support--; // In every row, the number of // dash counts is increased by 2 $dash_counter += 2; printf("\n"); } } // Driver code $n = 3; pattern($n); // This code is contributed by mits ?>
Javascript
<script> // javascript program to print the given pattern // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row function printdashes(k) { var i; for (i = 1; i <= k; i++) document.write("-"); } // function to print the pattern function pattern(n) { // variables for vertical left half /* 1*2*3* --4*5* ----6* */ var row, column, dashes = 0; var i, j, dash_counter = 0; var value = 1; // variables for vertical right half /* 10*11*12 *8*9 7 */ var k, l, decrementor = 0; var column_decrementor = 0; var support = n - 1; var temp = ((n * n) + 1); var temp1 = (n * 2) - 1; var z = temp; var tracker = 0; for (i = 1; i <= n; i++) { printdashes(dash_counter); // This part will take care of the vertical // left half of the pattern for (j = 1; j <= (2 * n) - dash_counter; j++) { // Printing the "*" in even positions if (j % 2 == 0) document.write("*"); else { document.write(value); value++; } } // This part will take care of the vertical // right half of the pattern for (k = 1; k <= (temp1 - decrementor); k++) { // Printing the "*" in even positions if (k % 2 == 0) document.write("*"); else { if (k == 1) tracker = temp; document.write(temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; document.write("<br>"); } } // Driver code var n = 3; pattern(n); // This code is contributed by 29AjayKumar </script>
Producción :
1*2*3*10*11*12 --4*5*8*9 ----6*7
Otro enfoque :
C++
// C++ program to print the given pattern #include <iostream> using namespace std; // function to print the pattern void printPattern(int row) { int x = 1; int z = (row * row) + 1; int col = row == 1 ? 1 : (row * 4) - 1; for(int i = 1; i <= row; i++) { int t = z; for(int j = 1; j <= col - ((i - 1) * 2); j++) { if ((i * 2) - 2 >= j) { cout << "-"; } else { if(col == 1) { cout << x; } else if(j <= col/2 && j % 2 == 1) { cout << x; x++; } else if(j > col/2 && j % 2 == 1) { cout << t; t++; } else { cout << "*"; } } } z = (z - row) + i; cout << "\n"; } } // Driver code int main() { int row = 3; printPattern(row); return 0; } // This code is contributed by shubhamsingh10
C
// C program to print the given pattern #include<stdio.h> // function to print the pattern void printPattern(int row) { int x = 1; int z = (row * row) + 1; int col = row == 1 ? 1 : (row * 4) - 1; for(int i = 1; i <= row; i++) { int t = z; for(int j = 1; j <= col - ((i - 1) * 2); j++) { if ((i * 2) - 2 >= j) { printf("-"); } else { if(col == 1) { printf("%d", x); } else if(j <= col/2 && j % 2 == 1) { printf("%d", x); x++; } else if(j > col/2 && j % 2 == 1) { printf("%d", t);; t++; } else { printf("*"); } } } z = (z - row) + i; printf("\n"); } } // Driver code int main() { int row = 3; printPattern(row); return 0; } // This code is contributed by ankurmishra1794
Java
// Java program to print the given pattern class GFG { // function to print the pattern static void printPattern(int row) { int x = 1; int z = (row * row) + 1; int col = row == 1 ? 1 : (row * 4) - 1; for(int i = 1; i <= row; i++) { int t = z; for(int j = 1; j <= col -((i - 1) * 2); j++) { if ((i * 2) - 2 >= j) { System.out.print("-"); } else { if(col == 1) { System.out.print(x); } else if(j <= col/2 && j % 2 == 1) { System.out.print(x); x++; } else if(j > col/2 && j % 2 == 1) { System.out.print(t); t++; } else { System.out.print("*"); } } } z = (z - row) + i; System.out.print("\n"); } } // Driver code public static void main(String[] args) { int row = 3; printPattern(row); } } /* This code is contributed by PrinciRaj1992 */
Python3
# Python3 program to print the given pattern # Function to print the pattern def printPattern(row): x = 1 z = (row * row) + 1 if row == 1: col = 1 else: col = (row * 4) - 1 for i in range(1, row + 1): t = z for j in range(1, col - ((i - 1) * 2) + 1): if ((i * 2) - 2 >= j): print("", end = "-") else: if (col == 1): print(x, end = "") elif (j <= col / 2 and j % 2 == 1): print(x, end = "") x += 1 elif (j > col / 2 and j % 2 == 1): print(t, end = "") t += 1 else: print("*", end = "") z = (z - row) + i print() # Driver code row = 3 printPattern(row) # This code is contributed by shivani
C#
// C# program to print the given pattern using System; class GFG { // function to print the pattern static void printPattern(int row) { int x = 1; int z = (row * row) + 1; int col = row == 1 ? 1 : (row * 4) - 1; for(int i = 1; i <= row; i++) { int t = z; for(int j = 1; j <= col -((i - 1) * 2); j++) { if ((i * 2) - 2 >= j) { Console.Write("-"); } else { if(col == 1) { Console.Write(x); } else if(j <= col/2 && j % 2 == 1) { Console.Write(x); x++; } else if(j > col/2 && j % 2 == 1) { Console.Write(t); t++; } else { Console.Write("*"); } } } z = (z - row) + i; Console.Write("\n"); } } // Driver code public static void Main(String[] args) { int row = 3; printPattern(row); } } // This code is contributed by 29AjayKumar
Javascript
<script> // javascript program to print the given pattern // function to print the pattern function printPattern(row) { var x = 1; var z = (row * row) + 1; var col = row == 1 ? 1 : (row * 4) - 1; for(var i = 1; i <= row; i++) { var t = z; for(var j = 1; j <= col -((i - 1) * 2); j++) { if ((i * 2) - 2 >= j) { document.write("-"); } else { if(col == 1) { document.write(x); } else if(j <= col/2 && j % 2 == 1) { document.write(x); x++; } else if(j > col/2 && j % 2 == 1) { document.write(t); t++; } else { document.write("*"); } } } z = (z - row) + i; document.write("<br>"); } } // Driver code var row = 3; printPattern(row); // This code is contributed by 29AjayKumar </script>
Producción :
1*2*3*10*11*12 --4*5*8*9 ----6*7
Otro enfoque :
Python3
def pattern(n): size = n*(n+1) # prev1 will be used to keep track of last number # printed in left half of pattern prev1 = 0 # prev2 will be used to keep track of last number # printed in right half of pattern prev2 = size for i in range(n): # print the '-' print('-'*(2*i), end = '') # l1 to store numbers of left half to be printed l1 = [j for j in range(prev1+1, prev1+n-i+1)] # l2 to store numbers of right half to be printed l2 = [j for j in range(prev2-(n-i)+1,prev2+1)] # combine l1 and l2 and print the list separated by * print(*l1+l2, sep = '*') # decrease prev2 and increase prev1 prev2 -= (n-i) prev1 += (n-i) # driver program n = 3 pattern(n) # This code is contributed # by Akash Jain (ultrainstinct).
Producción :
1*2*3*10*11*12 --4*5*8*9 ----6*7
Otro enfoque :
Python3
n = 4 temp_number = (n*n)+n counter = 1 for i in range(n): temp_list = [] for j in range(n-i): temp_list.append(counter) temp_list.append(temp_number-counter+1) counter += 1 temp_list.sort() temp_list = [str(each) for each in temp_list] [print("--", end="") for k in range(i)] print("*".join(temp_list))
Producción:
1*2*3*4*17*18*19*20 --5*6*7*14*15*16 ----8*9*12*13 ------10*11
Este artículo es una contribución de MAZHAR IMAM KHAN . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA