📜  用两个空心三角形打印图案的程序

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

用两个空心三角形打印图案的程序

给定一个数字 n,任务是编写一个程序来使用“$”绘制以下模式。

$
       $ $ $
     $ $ $ $ $
   $ $ $ $ $ $ $
   $           $
   $ $       $ $
   $ $ $   $ $ $
   $ $ $ $ $ $ $
   $ $ $   $ $ $
   $ $       $ $
   $           $
   $ $ $ $ $ $ $
     $ $ $ $ $
       $ $ $
         $

方法:
上面的图案是通过将其分解成更小的图案来打印的:

  1. 上面的三角形
$
      $ $ $
    $ $ $ $ $
  $ $ $ $ $ $ $
  1. 这是在第 0 到第 n-1 行的 for 循环的帮助下完成的。首先打印空格,然后打印'$'。
  2. 中间的身体
$           $
   $ $       $ $
   $ $ $   $ $ $
   $ $ $ $ $ $ $
   $ $ $   $ $ $
   $ $       $ $
   $           $
  1. 这又分为 3 个较小的模式:
    • 上半身
$           $
   $ $       $ $
   $ $ $   $ $ $
  • 这是在第 0 到第 n-2 行的 for 循环的帮助下完成的。首先打印左边的'$',然后打印空格,然后打印右边的'$'。
  • 中间中间的身体
$ $ $ $ $ $ $
  • 这是在 for 循环 for row = n-1 的帮助下完成的。这里只为 i 0 到 (2n -1) 打印“$”。
  • 下半身
$ $ $   $ $ $
   $ $       $ $
   $           $
  • 这是在第 n 到 2n-1 行的 for 循环的帮助下完成的。首先打印左边的'$',然后打印空格,然后打印右边的'$'。
  1. 下三角
$ $ $ $ $ $ $
     $ $ $ $ $
       $ $ $
         $
  1. 这是在第 0 到第 n-1 行的 for 循环的帮助下完成的。这与上三角模式类似。

因此将绘制完整的图案。
下面的程序打印如上所示的模式:

C++
// C++ implementation of above approach
 
#include 
 
using namespace std;
 
void printPattern(int n)
    {
    // for upper triangle
  //     $
 //    $ $ $
 //  $ $ $ $ $
// $ $ $ $ $ $ $
 
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = r; i < n - 1; i++) {
                cout << " " ;
                cout << " " ;
                         }
 
            // for $
            for (int i = 0; i < 2 * r + 1; i++)
                cout << "$ " ;
 
            // new line
                cout << endl ;
        }
 
        // for middle body
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
        for (int r = 0; r < 2 * n - 1; r++) {
            if (r < n - 1) {
 
                // for body
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
 
 
                // for $
                for (int i = 0; i <= r; i++)
                    cout << "$ " ;
 
                // for space
                for (int i = 0; i <= 2 * (n - r - 1)-2; i++) {
                   cout << " " ;
                    cout << " " ;
                     
                }
 
                // for $
                for (int i = 0; i <= r; i++)
                    cout << "$ " ;
            }
            else if (r == n - 1) {
 
                // for body
                // $ $ $ $ $ $ $
 
                // for $
                for (int i = 0; i < 2 * r + 1; i++)
                    cout << "$ " ;
            }
            else {
 
                // for body
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    cout << "$ " ;
 
                // for space
                for (int i = n; i <= r; i++) {
                    cout << " " ;
                    cout << " " ;
                     
                }
 
                // for space
                for (int i = n; i < r; i++) {
                    cout << " " ;
                    cout << " " ;
                     
                }
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    cout << "$ " ;
            }
 
            // new line
            cout << endl ;
        }
 
        // for lower triangle
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
 
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = 0; i < r; i++){
                cout << " " ;
                cout << " " ;
                }
 
            // for $
            for (int i = 0; i < 2 * (n - r) - 1; i++)
                cout << "$ " ;
 
            // new line
                cout << endl ;
        }
    }
 
 
int main()
{
     
    int n = 4;
    printPattern(n);
    return 0;
     
    // This code is contributed by ANKITRAI1
}


Java
// Java implementation of above approach
public class GFG {
 
    public static void printPattern(int n)
    {
    // for upper triangle
  //     $
 //    $ $ $
 //  $ $ $ $ $
// $ $ $ $ $ $ $
 
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = r; i < n - 1; i++){
                System.out.print(" ");
                System.out.print(" ");
            }
 
            // for $
            for (int i = 0; i < 2 * r + 1; i++)
                System.out.print("$ ");
 
            // new line
            System.out.println();
        }
 
        // for middle body
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
 
        for (int r = 0; r < 2 * n - 1; r++) {
            if (r < n - 1) {
 
                // for body
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
 
                // for $
                for (int i = 0; i <= r; i++)
                    System.out.print("$ ");
 
                // for space
                for (int i = 0; i <= 2 * (n - r - 1) - 2; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
 
                // for $
                for (int i = 0; i <= r; i++)
                    System.out.print("$ ");
            }
            else if (r == n - 1) {
 
                // for body
                // $ $ $ $ $ $ $
 
                // for $
                for (int i = 0; i < 2 * r + 1; i++)
                    System.out.print("$ ");
            }
            else {
 
                // for body
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    System.out.print("$ ");
 
                // for space
                for (int i = n; i <= r; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
 
                // for space
                for (int i = n; i < r; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    System.out.print("$ ");
            }
 
            // new line
            System.out.println();
        }
 
        // for lower triangle
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
 
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = 0; i < r; i++){
                System.out.print(" ");
                System.out.print(" ");
            }
 
            // for $
            for (int i = 0; i < 2 * (n - r) - 1; i++)
                System.out.print("$ ");
 
            // new line
            System.out.println();
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int n = 4;
        printPattern(n);
    }
}


Python3
# Python 3 implementation of above approach
 
def printPattern(n):
    # for upper triangle
#     $
# $ $ $
# $ $ $ $ $
# $ $ $ $ $ $ $
 
        for r in range(0,n,1):
            # for space
            for i in range(r,n - 1,1):
                print(" ",end = " ")
 
            # for $
            for i in range(0,2 * r + 1,1):
                print("$",end = " ")
 
            # new line
            print("\n")
         
        # for middle body
        # $         $
        # $ $     $ $
        # $ $ $ $ $ $
        # $ $ $ $ $ $ $
        # $ $ $ $ $ $
        # $ $     $ $
        # $         $
        for r in range(0,2 * n - 1,1):
            if (r < n - 1):
                # for body
                # $         $
                # $ $     $ $
                # $ $ $ $ $ $
 
 
                # for $
                for i in range(0,r+1,1):
                    print("$",end = " ")
 
                # for space
                for i in range(0,2 * (n - r - 1)-2+1,1):
                    print(" ",end = " ")
                             
                # for $
                for i in range(0,r+1,1):
                    print("$", end = " ")
            elif (r == n - 1):
                # for body
                # $ $ $ $ $ $ $
 
                # for $
                for i in range(0,2 * r + 1,1):
                    print("$",end = " ")
             
            else:
                # for body
                # $ $ $ $ $ $
                # $ $     $ $
                # $         $
 
                # for $
                i = 2 * n - 1
                while(i > r):
                    print("$",end = " ")
                    i -= 1
 
                # for space
                for i in range(n,r+1,1):
                    print(" ",end = " ")            
                 
                # for space
                for i in range(n,r,1):
                    print(" ",end = " ")
         
                 
                # for $
                i = 2 * n - 1
                while(i > r):
                    print("$",end = " ")
                    i -=1
 
            # new line
            print("\n")
        # for lower triangle
        # $ $ $ $ $ $ $
        # $ $ $ $ $
        #     $ $ $
        #     $
 
        for r in range(0,n,1):
            # for space
            for i in range(0,r,1):
                print(" ",end = " ")
 
            # for $
            for i in range(0,2 * (n - r) - 1,1):
                print("$",end = " ")
 
            # new line
            print("\n")
# driver code
if __name__ == '__main__':
    n = 4
    printPattern(n)
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of above approach
using System;
 
class GFG {
 
    public static void printPattern(int n) {
         
        // for upper triangle
        //     $
        //    $ $ $
        //  $ $ $ $ $
        // $ $ $ $ $ $ $
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = r; i < n - 1; i++) {
                Console.Write(" ");
                Console.Write(" ");
            }
 
            // for $
            for (int i = 0; i < 2 * r + 1; i++)
            Console.Write("$ ");
 
            // new line
            Console.Write("\n");
        }
 
        // for middle body
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
 
        for (int r = 0; r < 2 * n - 1; r++) {
            if (r < n - 1) {
 
                // for body
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
 
                // for $
                for (int i = 0; i <= r; i++)
                Console.Write("$ ");
 
                // for space
                for (int i = 0; i <= 2 * (n - r - 1) - 2; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
 
                // for $
                for (int i = 0; i <= r; i++)
                Console.Write("$ ");
            }
            else if (r == n - 1) {
 
                // for body
                // $ $ $ $ $ $ $
 
                // for $
                for (int i = 0; i < 2 * r + 1; i++)
                Console.Write("$ ");
            }
            else {
 
                // for body
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                Console.Write("$ ");
 
                // for space
                for (int i = n; i <= r; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
 
                // for space
                for (int i = n; i < r; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
 
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                Console.Write("$ ");
            }
 
            // new line
            Console.Write("\n");
        }
 
        // for lower triangle
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
 
        for (int r = 0; r < n; r++) {
 
            // for space
            for (int i = 0; i < r; i++) {
                Console.Write(" ");
                Console.Write(" ");
            }
 
            // for $
            for (int i = 0; i < 2 * (n - r) - 1; i++)
            Console.Write("$ ");
 
            // new line
            Console.Write("\n");
        }
    }
 
    // Driver Code
    public static void Main() {
 
        int n = 4;
        printPattern(n);
    }
}
 
// This code is contributed by ChitraNayal


PHP
 $r; $i--)
                echo("$ ") ;
 
            // for space
            for ($i = $n; $i <= $r; $i++)
            {
                echo(" ") ;
                echo(" ") ;
            }
 
            // for space
            for ($i = $n; $i < $r; $i++)
            {
                echo (" ") ;
                echo (" ") ;
            }
 
            // for $
            for ($i = 2 * $n - 1; $i > $r; $i--)
                echo("$ ") ;
        }
 
        // new line
        echo("\n") ;
    }
 
    // for lower triangle
    // $ $ $ $ $ $ $
    // $ $ $ $ $
    // $ $ $
    // $
    for ($r = 0; $r < $n; $r++)
    {
 
        // for space
        for ($i = 0; $i < $r; $i++)
        {
            echo(" ") ;
            echo(" ") ;
        }
 
        // for $
        for ($i = 0; $i < 2 * ($n - $r) - 1; $i++)
            echo ("$ ") ;
 
        // new line
        echo ("\n") ;
    }
}
 
// Driver Code
$n = 4;
printPattern($n);
 
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript


输出:
$ 
    $ $ $ 
  $ $ $ $ $ 
$ $ $ $ $ $ $ 
$           $ 
$ $       $ $ 
$ $ $   $ $ $ 
$ $ $ $ $ $ $ 
$ $ $   $ $ $ 
$ $       $ $ 
$           $ 
$ $ $ $ $ $ $ 
  $ $ $ $ $ 
    $ $ $ 
      $