Tenemos que imprimir el patrón como se indica en el siguiente ejemplo.
Ejemplos:
Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1
C++
#include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { cout << " "; } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) cout << "*"; else cout << count++; } cout << "\n"; if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code int main() { int n = 5; display(n); return 0; } // This code is contributed by vt_m
Java
// Java program to print above pattern import java.util.Scanner; class Pattern { void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { System.out.print(" "); } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) System.out.print("*"); else System.out.print(count++); } System.out.println(); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code public static void main(String[] args) { int n = 5; Pattern p = new Pattern(); p.display(n); } }
Python3
# Python3 program to print above pattern def display(n): # 'sp' used for space and # 'st' used for star sp = n // 2 st = 1 # Outer for loop prints number # of lines for i in range(1, n + 1): for j in range(1, sp + 1): print(end = " ") count = 1 for k in range(1, st + 1): if (k % 2 == 0): print("*", end = "") else: print(count, end = "") count += 1 print() if (i <= n // 2): # Before reaching to half after # every line space is decreased # by 1 and star is increased by 2 sp = sp - 1 st = st + 2 else: # After reaching to half # space is increased by 1 # and star is decreased by 2 sp = sp + 1 st = st - 2 # Driver Code n = 5 display(n) # This code is contributed by # Mohit kumar 29
C#
// C# program to print above pattern using System; class Pattern { void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { Console.Write(" "); } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) Console.Write("*"); else Console.Write(count++); } Console.WriteLine(); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code public static void Main() { int n = 5; Pattern p = new Pattern(); p.display(n); } } //This code is contributed by vt_m.
PHP
<?php // php program to print // above pattern function display($n) { // 'sp' used for space and // 'st' used for star $sp = $n / 2; $st = 1; // Outer for loop prints // number of lines for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $sp; $j++) { echo " "; } $count = 1; for ($k = 1; $k <= $st; $k++) { if ($k % 2 == 0) echo "*"; else echo $count++; } echo "\n"; if ($i <= $n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 $sp = $sp - 1; $st = $st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 $sp = $sp + 1; $st = $st - 2; } } } // Driver Code $n = 5; display($n); // This code is contributed by mits ?>
Javascript
<script> // JavaScript program to print above pattern function display(n) { // 'sp' used for space and 'st' used for star var sp = n / 2, st = 1; // Outer for loop prints number of lines for (var i = 1; i <= n; i++) { for (var j = 1; j <= sp; j++) { document.write(" "); } var count = 1; for (var k = 1; k <= st; k++) { if (k % 2 == 0) document.write("*"); else document.write(count++); } document.write("<br>"); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code var n = 5; display(n); </script>
Producción:
1 1*2 1*2*3 1*2 1
Complejidad de tiempo: O(n 2 ), donde n representa la entrada dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Publicación traducida automáticamente
Artículo escrito por Bishal Kumar Dubey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA