📜  程序打印婴儿床图案

📅  最后修改于: 2021-05-28 02:15:58             🧑  作者: Mango

给定的任务是使用’$’绘制婴儿床图案

婴儿床床型:

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

下面是实现上述问题的代码:

程序 :

C++
// C++ program to print
// the Cot Bed Pattern
  
// header files
#include 
using namespace std;
  
// function to print the pattern
void pattern(int nos, int i, int space)
{
    char prt = '$';
    int s, j;
  
    // loop for the spacing between the $ sign
    for (s = nos; s >= 1; s--)
    {
        cout <<" ";
    }
  
    // skipping the $
    for (j = 1; j <= i; j++) 
    {
        if (space != 0) 
        {
            if (i == 9 && j == 1) 
            {
                continue;
            }
        }
  
        // printing the $
        if (i == 1 || i == 9)
        {
            cout << prt;
        }
        else if (j == 1 || j == i)
        {
            cout << prt;
        }
        else
        {
            cout <<" ";
        }
    }
}
  
// Driver code
int main()
{
    int i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1; (i = i - 2))
    {
  
        // printing the first upper triangle
        pattern(nos, i, 0);
  
        // printing the second upper triangle
        pattern(nosp, i, 1);
  
        // printing the third upper triangle
        pattern(nbsp, i, 1);
  
        cout << endl;
        nos++;
        nosp = nosp + 2;
        nbsp = nbsp + 2;
    }
  
    nos = 3, nosp = 5, nbsp = 5;
  
    for (i = 3; i <= 9; (i = i + 2))
    {
  
        // printing the first lower triangle
        pattern(nos, i, 0);
  
        // printing the second lower triangle
        pattern(nosp, i, 1);
  
        // printing the third lower triangle
        pattern(nbsp, i, 1);
  
        cout << endl;
        nos--;
        nosp = nosp - 2;
        nbsp = nbsp - 2;
    }
  
    return 0;
}
  
// This code is contributed by SHUBHAMSINGH10


C
// C program to print
// the Cot Bed Pattern
  
// header files
#include 
#include 
#include 
  
// function to print the pattern
void pattern(int nos, int i, int space)
{
    char prt = '$';
    int s, j;
  
    // loop for the spacing between the $ sign
    for (s = nos; s >= 1; s--) {
        printf("  ");
    }
  
    // skipping the $
    for (j = 1; j <= i; j++) {
        if (space != 0) {
            if (i == 9 && j == 1) {
                continue;
            }
        }
  
        // printing the $
        if (i == 1 || i == 9) {
            printf("%c", prt);
        }
        else if (j == 1 || j == i) {
            printf("%c", prt);
        }
        else {
            printf("  ");
        }
    }
}
// Driver code
int main()
{
    int i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1; (i = i - 2)) {
  
        // printing the first upper triangle
        pattern(nos, i, 0);
  
        // printing the second upper triangle
        pattern(nosp, i, 1);
  
        // printing the third upper triangle
        pattern(nbsp, i, 1);
  
        printf("\n");
        nos++;
        nosp = nosp + 2;
        nbsp = nbsp + 2;
    }
  
    nos = 3, nosp = 5, nbsp = 5;
  
    for (i = 3; i <= 9; (i = i + 2)) {
  
        // printing the first lower triangle
        pattern(nos, i, 0);
  
        // printing the second lower triangle
        pattern(nosp, i, 1);
  
        // printing the third lower triangle
        pattern(nbsp, i, 1);
  
        printf("\n");
        nos--;
        nosp = nosp - 2;
        nbsp = nbsp - 2;
    }
  
    return 0;
}


Java
// Java program to print 
// the Cot Bed Pattern 
  
public class Improve {
      
    // function to print the pattern 
    static void pattern(int nos, int i, int space) 
    { 
        char prt = '$'; 
        int s, j; 
        
        // loop for the spacing between the $ sign 
        for (s = nos; s >= 1; s--) { 
            System.out.print("  "); 
        } 
        
        // skipping the $ 
        for (j = 1; j <= i; j++) { 
            if (space != 0) { 
                if (i == 9 && j == 1) { 
                    continue; 
                } 
            } 
        
            // printing the $ 
            if (i == 1 || i == 9) { 
                System.out.print(prt + ""); 
            } 
            else if (j == 1 || j == i) { 
                System.out.print(prt + ""); 
            } 
            else { 
                System.out.print("  "); 
            } 
        } 
    } 
    // Driver code  
    public static void main(String args[])
    {
           int i, nos = 0, nosp = -1, nbsp = -1; 
            for (i = 9; i >= 1;  i -= 2) { 
            
                // printing the first upper triangle 
                pattern(nos, i, 0); 
            
                // printing the second upper triangle 
                pattern(nosp, i, 1); 
            
                // printing the third upper triangle 
                pattern(nbsp, i, 1); 
            
                System.out.println(); 
                nos++; 
                nosp = nosp + 2; 
                nbsp = nbsp + 2; 
            } 
            
             nos = 3;
             nosp = 5;
             nbsp = 5 ; 
            
            for (i = 3; i <= 9; i += 2) { 
            
                // printing the first lower triangle 
                pattern(nos, i, 0); 
            
                // printing the second lower triangle 
                pattern(nosp, i, 1); 
            
                // printing the third lower triangle 
                pattern(nbsp, i, 1); 
            
                System.out.println(); 
                nos--; 
                nosp = nosp - 2; 
                nbsp = nbsp - 2; 
            } 
            
    }
    // This Code is contributed by ANKITRAI1
}


Python 3
# Python 3 program to print
# the Cot Bed Pattern
   
# function to print the pattern
def pattern(nos, i, space):
  
    prt = '$'
   
    # loop for the spacing between the $ sign
    for s in range(nos, 0, -1) :
        print(end="  ")
   
    # skipping the $
    for j in range(1, i+1) :
        if (space != 0) :
            if (i == 9 and j == 1) :
                continue
   
        # printing the $
        if (i == 1 or i == 9) :
            print(prt,end="")
          
        elif (j == 1 or j == i) :
            print(prt,end="")
          
        else :
            print(end="  ")
          
# Driver code
if __name__ == "__main__":
      
    nos = 0
    nosp = -1
    nbsp = -1
    for i in range( 9, 0 , -2) :
   
        # printing the first upper triangle
        pattern(nos, i, 0)
   
        # printing the second upper triangle
        pattern(nosp, i, 1)
   
        # printing the third upper triangle
        pattern(nbsp, i, 1)
   
        print()
        nos += 1
        nosp = nosp + 2
        nbsp = nbsp + 2
   
    nos = 3
    nosp = 5
    nbsp = 5
   
    for i in range(3, 10,  2) :
   
        # printing the first lower triangle
        pattern(nos, i, 0)
   
        # printing the second lower triangle
        pattern(nosp, i, 1)
   
        # printing the third lower triangle
        pattern(nbsp, i, 1)
   
        print()
        nos -= 1
        nosp = nosp - 2
        nbsp = nbsp - 2
  
# This code is contributed by
# ChitraNayal


C#
// C#  program to print 
// the Cot Bed Pattern 
using System;
  
public class GFG{
      
    // function to print the pattern 
    static void pattern(int nos, int i, int space) 
    { 
        char prt = '$'; 
        int s, j; 
          
        // loop for the spacing between the $ sign 
        for (s = nos; s >= 1; s--) { 
            Console.Write(" "); 
        } 
          
        // skipping the $ 
        for (j = 1; j <= i; j++) { 
            if (space != 0) { 
                if (i == 9 && j == 1) { 
                    continue; 
                } 
            } 
          
            // printing the $ 
            if (i == 1 || i == 9) { 
                Console.Write(prt + ""); 
            } 
            else if (j == 1 || j == i) { 
                Console.Write(prt + ""); 
            } 
            else { 
                Console.Write(" "); 
            } 
        } 
    } 
          
    // Driver code
    static public void Main (){
            int i, nos = 0, nosp = -1, nbsp = -1; 
            for (i = 9; i >= 1; i -= 2) { 
              
                // printing the first upper triangle 
                pattern(nos, i, 0); 
              
                // printing the second upper triangle 
                pattern(nosp, i, 1); 
              
                // printing the third upper triangle 
                pattern(nbsp, i, 1); 
              
                Console.WriteLine(); 
                nos++; 
                nosp = nosp + 2; 
                nbsp = nbsp + 2; 
            } 
              
            nos = 3; 
            nosp = 5; 
            nbsp = 5 ; 
              
            for (i = 3; i <= 9; i += 2) { 
              
                // printing the first lower triangle 
                pattern(nos, i, 0); 
              
                // printing the second lower triangle 
                pattern(nosp, i, 1); 
              
                // printing the third lower triangle 
                pattern(nbsp, i, 1); 
              
                Console.WriteLine(); 
                nos--; 
                nosp = nosp - 2; 
                nbsp = nbsp - 2; 
            } 
              
    } 
    // This Code is contributed by Sachin
}


PHP
= 1; $s--) 
    { 
        echo (" "); 
    } 
  
    // skipping the $ 
    for ($j = 1; $j <= $i; $j++) 
    { 
        if ($space != 0) 
        { 
            if ($i == 9 && $j == 1) 
            { 
                continue; 
            } 
        } 
  
        // printing the $ 
        if ($i == 1 || $i == 9) 
        { 
            echo $prt; 
        } 
        else if ($j == 1 || $j == $i) 
        { 
            echo $prt; 
        } 
        else
        { 
            echo (" "); 
        } 
    } 
} 
  
// Driver code 
$nos = 0;
$nosp = -1;
$nbsp = -1; 
for ($i = 9; $i >= 1; ($i = $i - 2))
{ 
  
    // printing the first upper triangle 
    pattern($nos, $i, 0); 
  
    // printing the second upper triangle 
    pattern($nosp, $i, 1); 
  
    // printing the third upper triangle 
    pattern($nbsp, $i, 1); 
  
    echo ("\n"); 
    $nos++; 
    $nosp = $nosp + 2; 
    $nbsp = $nbsp + 2; 
} 
  
$nos = 3; $nosp = 5; $nbsp = 5; 
  
for ($i = 3; $i <= 9; ($i = $i + 2))
{ 
  
    // printing the first lower triangle 
    pattern($nos, $i, 0); 
  
    // printing the second lower triangle 
    pattern($nosp, $i, 1); 
  
    // printing the third lower triangle 
    pattern($nbsp, $i, 1); 
  
    echo ("\n"); 
    $nos--; 
    $nosp = $nosp - 2; 
    $nbsp = $nbsp - 2; 
}
  
// This code is contributed 
// by Sach_Code
?>


输出 :

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

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。