📜  打印左右箭头图案的程序

📅  最后修改于: 2022-05-13 01:57:59.158000             🧑  作者: Mango

打印左右箭头图案的程序

编写程序打印由星星组成的右箭头图案和左箭头图案。输入是一个奇数 n,代表要打印的图案的高度和宽度。
例子:

Input : n = 9
Output :
    *
     *               
      *  
       *
*********
       *
      *
     *
    *


    *
   *
  *
 *
*********
 * 
  *
   *
    *


Input : n = 7
Output :
   *
    *
     *
*******
     *
    *
   *


   *
  *
 *
*******
 *
  *
   *

C++
// C++ program to print Pyramid pattern
#include 
using namespace std;
 
// function to print right arrow pattern
void rightpattern(int n)
{
    // for printing upper portion
    int c1 = (n - 1) / 2;
 
    // for printing lower portion
    int c2 = 3 * n / 2 - 1;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
 
            // checking conditions to print right arrow
            if (j - i == c1 || i + j == c2 || i == c1)
                cout << "*";
 
            // otherwise print space
            else
                cout << " ";
        }
        // for jumping to next line
        cout << "\n";
    }
}
 
// function to print left arrow pattern
void leftpattern(int n)
{
    char s = ' ';
    char st = '*';
 
    // for printing upper part
    for (int i = (n - 1) / 2; i > 0; i--)
        cout << string(i, s) << st << endl;
 
    // for printing middle part
    for (int i = 0; i < n; i++)
        cout << "*";
    cout << endl;
 
    // for printing lower part
    for (int i = 1; i <= (n - 1) / 2; i++)
        cout << string(i, s) << st << endl;
    cout << endl;
}
 
// Driver program
int main()
{
    int n = 9; // Must be odd
 
    // function calling to print right arrow
    rightpattern(n);
 
    cout << endl  << endl;
 
    // function calling to print left arrow
    leftpattern(n);
 
    return 0;
}


Java
// Java program to print Pyramid pattern
class GFG
{
 
// function to print right arrow pattern
static void rightpattern(int n)
{
    // for printing upper portion
    int c1 = (n - 1) / 2;
 
    // for printing lower portion
    int c2 = 3 * n / 2 - 1;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
 
            // checking conditions to print
            // right arrow
            if (j - i == c1 ||
                i + j == c2 || i == c1)
            {
                System.out.print("*");
            } // otherwise print space
            else
            {
                System.out.print(" ");
            }
        }
        // for jumping to next line
        System.out.print("\n");
    }
}
 
static void string(int n)
{
    for (int i = n; i > 0; i--)
    {
        System.out.print(" ");
    }
}
 
// function to print left arrow pattern
static void leftpattern(int n)
{
    char s = ' ';
    char st = '*';
 
    // for printing upper part
    for (int i = (n - 1) / 2; i > 0; i--)
    {
        string(i);
        System.out.println(st);
    }
     
    // for printing middle part
    for (int i = 0; i < n; i++)
    {
        System.out.print("*");
    }
    System.out.println();
 
    // for printing lower part
    for (int i = 1; i <= (n - 1) / 2; i++)
    {
        string(i);
        System.out.println(st);
    }
    System.out.println();
}
 
// Driver Code
public static void main(String args[])
{
    int n = 9; // Must be odd
 
    // function calling to print
    // right arrow
    rightpattern(n);
 
    System.out.println("\n");
 
    // function calling to print
    // left arrow
    leftpattern(n);
}
}
 
// This code is contributed
// by PrinciRaj1992


Python3
# Python3 program to print Pyramid pattern
 
# function to print right arrow pattern
def rightpattern(n):
     
    # for printing upper portion
    c1 = (n - 1) // 2;
 
    # for printing lower portion
    c2 = 3 * n // 2 - 1;
 
    for i in range(n):
 
        for j in range(n):
 
            # checking conditions to print
            # right arrow
            if (j - i == c1 or
                i + j == c2 or i == c1):
 
                print("*", end = "");
                 
            # otherwise print space
            else:
 
                print(" ", end = "");
 
        # for jumping to next line
        print();
 
def string(n):
 
    for i in range(n):
        print(" ", end = "");
 
# function to print left arrow pattern
def leftpattern(n):
 
    s = ' ';
    st = '*';
 
    # for printing upper part
    for i in range((n - 1) // 2, 0, -1):
        string(i);
        print(st);
 
    # for printing middle part
    for i in range(n):
        print("*", end = "");
 
    print();
 
    # for printing lower part
    for i in range(1, ((n - 1) // 2) + 1):
        string(i);
        print(st);
 
    print();
 
# Driver Code
if __name__ == '__main__':
    n = 9; # Must be odd
 
    # function calling to print
    # right arrow
    rightpattern(n);
 
    print("");
 
    # function calling to print
    # left arrow
    leftpattern(n);
 
# This code is contributed by PrinciRaj1992


C#
// C# program to print Pyramid pattern
using System;
 
class GFG
{
 
// function to print right arrow pattern
static void rightpattern(int n)
{
    // for printing upper portion
    int c1 = (n - 1) / 2;
 
    // for printing lower portion
    int c2 = 3 * n / 2 - 1;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
 
            // checking conditions to print
            // right arrow
            if (j - i == c1 ||
                i + j == c2 || i == c1)
            {
                Console.Write("*");
            } // otherwise print space
            else
            {
                Console.Write(" ");
            }
        }
        // for jumping to next line
        Console.Write("\n");
    }
}
 
static void String(int n)
{
    for (int i = n; i > 0; i--)
    {
        Console.Write(" ");
    }
}
 
// function to print left arrow pattern
static void leftpattern(int n)
{
    char s = ' ';
    char st = '*';
 
    // for printing upper part
    for (int i = (n - 1) / 2; i > 0; i--)
    {
        String(i);
        Console.WriteLine(st);
    }
     
    // for printing middle part
    for (int i = 0; i < n; i++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
 
    // for printing lower part
    for (int i = 1; i <= (n - 1) / 2; i++)
    {
        String(i);
        Console.WriteLine(st);
    }
    Console.WriteLine();
}
 
// Driver Code
public static void Main()
{
    int n = 9; // Must be odd
 
    // function calling to print
    // right arrow
    rightpattern(n);
 
    Console.WriteLine("\n");
 
    // function calling to print
    // left arrow
    leftpattern(n);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
 0; $i--)
    {
    for($j = 0; $j < $i; $j++)
        echo " ";
    echo $st."\n";
    }
     
    // for printing middle part
    for ($i = 0; $i < $n; $i++)
        echo "*";
    echo "\n";
 
    // for printing lower part
    for ($i = 1; $i <= ($n - 1) / 2; $i++)
    {
            for($j = 0; $j < $i; $j++)
        echo " ";
    echo $st."\n";
    }
    echo "\n";
}
 
    // Driver Code
    $n = 9; // Must be odd
 
    // function calling to
    // print right arrow
    rightpattern($n);
 
    echo "\n\n";
 
    // function calling to
    // print left arrow
    leftpattern($n);
 
 
// This code is contributed by mits
 
?>


Javascript


输出:

*
     *               
      *  
       *
*********
       *
      *
     *
    *


    *
   *
  *
 *
*********
 *
  *
   *
    *

时间复杂度: O(n^2)