📜  打印小屋星形图案的程序

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

打印小屋星形图案的程序

给定一个数字 N,使得 N >= 5。任务是打印具有 (N + 3) 行的小屋星形图案,如下例所示。

例子:

Input: N = 5
Output:
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
* * *       * * * 
* * *       * * * 
* * *       * * * 

Input: N = 7
Output:
            * 
          * * * 
        * * * * * 
      * * * * * * * 
    * * * * * * * * * 
  * * * * * * * * * * * 
* * * * * * * * * * * * * 
* * *               * * * 
* * *               * * * 
* * *               * * * 

方法:图案的上面 N 行将是一个三角形,最后 3 行将包含两个空格分隔的矩形,如上面的示例所示。

  1. 首先打印 N 行的上三角形。
  2. 然后将小屋的 2 个下部矩形打印成 3 行。

下面是上述方法的实现:

C++
// C++ program to print
// the Hut Star Pattern
 
#include 
 
using namespace std;
 
// Function to print the Hut-Star Pattern
void printHutStar(int n)
{
    int i, j;
 
    // Printing the upper triangle
    for (i = 0; i < n; i++) {
 
        // Left space triangle
        for (j = i + 1; j < n; j++)
            cout << " " ;
 
        // Center Star triangle
        for (j = 0; j < (2 * i + 1); j++)
            cout << "*" ;
 
        cout << endl ;
    }
 
    // Printing Lower rectangles
    for (i = 0; i < 3; i++) {
 
        // Left rectangle
        for (j = 0; j < 3; j++)
            cout << "*" ;
 
        // Center Space rectangle
        for (j = 0; j < (2 * n - 7); j++)
            cout << " " ;
 
        // Right rectangle
        for (j = 0; j < 3; j++)
            cout << "*" ;
 
        cout << endl ;
    }
}
 
 
int main()
{
int n = 7;
 
    // function calling
    printHutStar(n);
 
return 0;
// This code is contributed
// by ANKITRAI1
}


Java
// Java program to print
// the Hut Star Pattern
   
public class GFG {
   
    // Function to print the Hut-Star Pattern
    static void printHutStar(int n)
    {
        int i, j;
   
        // Printing the upper triangle
        for (i = 0; i < n; i++) {
   
            // Left space triangle
            for (j = i + 1; j < n; j++)
                System.out.print(" ");
   
            // Center Star triangle
            for (j = 0; j < (2 * i + 1); j++)
                System.out.print("*");
   
            System.out.println();
        }
   
        // Printing Lower rectangles
        for (i = 0; i < 3; i++) {
   
            // Left rectangle
            for (j = 0; j < 3; j++)
                System.out.print("*");
   
            // Center Space rectangle
            for (j = 0; j < (2 * n - 7); j++)
                System.out.print(" ");
   
            // Right rectangle
            for (j = 0; j < 3; j++)
                System.out.print("*");
   
            System.out.println();
        }
    }
   
    // Driver Code
    public static void main(String args[])
    {
        int n = 7;
   
        printHutStar(n);
    }
}


Python3
# Python program to print
# the Hut Star Pattern
 
# Function to print the Hut-Star Pattern
def printHutStar(n):
 
    # Printing the upper triangle
    for i in range(n):
 
        # Left space triangle
        for j in range(i + 1, n):
            print(' ', end = '')
 
        # Center Star triangle
        for j in range(0, 2 * i + 1):
            print('*', end = '')
        print()
 
    # Printing the lower rectangles
    for i in range(3):
 
        # Left rectangle
        for j in range(3):
            print('*', end = '')
  
        # Center Space rectangle
        for j in range(2 * n - 7):
            print(' ', end = '')
 
        # Right rectangle
        for j in range(3):
            print('*', end = '')
        print()
 
# Driver Code
n = 7
 
# function calling
printHutStar(n)
 
# This code is contributed
# by SamyuktaSHegde


C#
// C# program to print
// the Hut Star Pattern
using System;
 
class GFG {
 
    // Function to print the Hut-Star Pattern
    static void printHutStar(int n)
    {
        int i, j;
 
        // Printing the upper triangle
        for (i = 0; i < n; i++) {
 
            // Left space triangle
            for (j = i + 1; j < n; j++)
                Console.Write(" ");
 
            // Center Star triangle
            for (j = 0; j < (2 * i + 1); j++)
                Console.Write("*");
 
            Console.Write("\n");
        }
 
        // Printing Lower rectangles
        for (i = 0; i < 3; i++) {
 
            // Left rectangle
            for (j = 0; j < 3; j++)
                Console.Write("*");
 
            // Center Space rectangle
            for (j = 0; j < (2 * n - 7); j++)
                Console.Write(" ");
 
            // Right rectangle
            for (j = 0; j < 3; j++)
                Console.Write("*");
 
            Console.Write("\n");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 7;
 
        printHutStar(n);
    }
}
 
// This code is contributed by ChitraNayal


PHP


Javascript


C++
// C++ program to print
// the Hut Star Pattern
 
#include
using namespace std;
 
int main()
{
    int n=7,i,j;
     
    //LOOP FOR HUT PYRAMID
    for(i=0,j=0;i=n-1)
        {
            cout<<"*";
        }
        else
            cout<<" ";
    }
     
    //LOOPS FOR LOWER WALLS
    for(int k=0;k<3;k++)        
    {
        // Left and right rectangle
        for(int l=0;l=n-4+i))
                cout<<"*";
            else
                cout<<" ";
        }
        cout<


Java
// Java program to print the Hut Star Pattern
class GFG
{
 
public static void main(String args[])
{
    int n = 7, i, j;
 
    // LOOP FOR HUT PYRAMID
    for (i = 0, j = 0; i < n; j++)
    {
        if (j == n + i)
        {
            j = -1;
            ++i;
            System.out.print("\n");
        } // print Center Star triangle
        else if ((i + j) >= n - 1)
        {
            System.out.print("*");
        }
        else
        {
            System.out.print(" ");
        }
    }
 
    // LOOPS FOR LOWER WALLS
    for (int k = 0; k < 3; k++)
    {
        // Left and right rectangle
        for (int l = 0; l < n - 1 + i; l++)
        {
            if (l <= 2 || (l <= n - 1 + i &&
                           l >= n - 4 + i))
            {
                System.out.print("*");
            }
            else
            {
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
}
}
 
// This code is contributed
// by PrinciRaj1992


Python3
# Python3 program to print the
# Hut Star Pattern
import math as mt
 
if __name__ == '__main__':
 
    n = 7
    i, j = 0, 0
     
    # LOOP FOR HUT PYRAMID
    while(i < n):    
        if(j == n + i):
            j = -1
            i += 1
            print()
             
        elif((i + j) >= n - 1):
            print("*", end = "")
        else:
            print(end = " ")
        j += 1
     
    # LOOPS FOR LOWER WALLS
    for k in range(3):   
         
        # Left and right rectangle
        for l in range(n - 1 + i):
            if(l <= 2 or (l <= n - 1 + i and
                          l >= n - 4 + i)):
                print("*", end = "")
            else:
                print(end = " ")
         
        print()
     
# This code is contributed by
# Mohit kumar 29


C#
// C# program to print the Hut Star Pattern
using System;
 
class GFG
{
 
public static void Main()
{
    int n = 7, i, j;
 
    // LOOP FOR HUT PYRAMID
    for (i = 0, j = 0; i < n; j++)
    {
        if (j == n + i)
        {
            j = -1;
            ++i;
            Console.Write("\n");
        } // print Center Star triangle
        else if ((i + j) >= n - 1)
        {
            Console.Write("*");
        }
        else
        {
            Console.Write(" ");
        }
    }
 
    // LOOPS FOR LOWER WALLS
    for (int k = 0; k < 3; k++)
    {
        // Left and right rectangle
        for (int l = 0; l < n - 1 + i; l++)
        {
            if (l <= 2 || (l <= n - 1 + i &&
                        l >= n - 4 + i))
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
        Console.Write("\n");
    }
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
= $n - 1)
    {
        echo("*");
    }
    else
    {
        echo(" ");
    }
}
 
// LOOPS FOR LOWER WALLS
for ($k = 0; $k < 3; $k++)
{
    // Left and right rectangle
    for ($l = 0; $l < $n - 1 + $i; $l++)
    {
        if ($l <= 2 || ($l <= $n - 1 + $i &&
                        $l >= $n - 4 + $i))
        {
            echo("*");
        }
        else
        {
            echo(" ");
        }
    }
    echo("\n");
}
 
// This code is contributed
// by Mukul singh
?>


Javascript


输出:
*
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***

另一种方法:(降低时间复杂度)

C++

// C++ program to print
// the Hut Star Pattern
 
#include
using namespace std;
 
int main()
{
    int n=7,i,j;
     
    //LOOP FOR HUT PYRAMID
    for(i=0,j=0;i=n-1)
        {
            cout<<"*";
        }
        else
            cout<<" ";
    }
     
    //LOOPS FOR LOWER WALLS
    for(int k=0;k<3;k++)        
    {
        // Left and right rectangle
        for(int l=0;l=n-4+i))
                cout<<"*";
            else
                cout<<" ";
        }
        cout<

Java

// Java program to print the Hut Star Pattern
class GFG
{
 
public static void main(String args[])
{
    int n = 7, i, j;
 
    // LOOP FOR HUT PYRAMID
    for (i = 0, j = 0; i < n; j++)
    {
        if (j == n + i)
        {
            j = -1;
            ++i;
            System.out.print("\n");
        } // print Center Star triangle
        else if ((i + j) >= n - 1)
        {
            System.out.print("*");
        }
        else
        {
            System.out.print(" ");
        }
    }
 
    // LOOPS FOR LOWER WALLS
    for (int k = 0; k < 3; k++)
    {
        // Left and right rectangle
        for (int l = 0; l < n - 1 + i; l++)
        {
            if (l <= 2 || (l <= n - 1 + i &&
                           l >= n - 4 + i))
            {
                System.out.print("*");
            }
            else
            {
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
}
}
 
// This code is contributed
// by PrinciRaj1992

Python3

# Python3 program to print the
# Hut Star Pattern
import math as mt
 
if __name__ == '__main__':
 
    n = 7
    i, j = 0, 0
     
    # LOOP FOR HUT PYRAMID
    while(i < n):    
        if(j == n + i):
            j = -1
            i += 1
            print()
             
        elif((i + j) >= n - 1):
            print("*", end = "")
        else:
            print(end = " ")
        j += 1
     
    # LOOPS FOR LOWER WALLS
    for k in range(3):   
         
        # Left and right rectangle
        for l in range(n - 1 + i):
            if(l <= 2 or (l <= n - 1 + i and
                          l >= n - 4 + i)):
                print("*", end = "")
            else:
                print(end = " ")
         
        print()
     
# This code is contributed by
# Mohit kumar 29

C#

// C# program to print the Hut Star Pattern
using System;
 
class GFG
{
 
public static void Main()
{
    int n = 7, i, j;
 
    // LOOP FOR HUT PYRAMID
    for (i = 0, j = 0; i < n; j++)
    {
        if (j == n + i)
        {
            j = -1;
            ++i;
            Console.Write("\n");
        } // print Center Star triangle
        else if ((i + j) >= n - 1)
        {
            Console.Write("*");
        }
        else
        {
            Console.Write(" ");
        }
    }
 
    // LOOPS FOR LOWER WALLS
    for (int k = 0; k < 3; k++)
    {
        // Left and right rectangle
        for (int l = 0; l < n - 1 + i; l++)
        {
            if (l <= 2 || (l <= n - 1 + i &&
                        l >= n - 4 + i))
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
        Console.Write("\n");
    }
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

= $n - 1)
    {
        echo("*");
    }
    else
    {
        echo(" ");
    }
}
 
// LOOPS FOR LOWER WALLS
for ($k = 0; $k < 3; $k++)
{
    // Left and right rectangle
    for ($l = 0; $l < $n - 1 + $i; $l++)
    {
        if ($l <= 2 || ($l <= $n - 1 + $i &&
                        $l >= $n - 4 + $i))
        {
            echo("*");
        }
        else
        {
            echo(" ");
        }
    }
    echo("\n");
}
 
// This code is contributed
// by Mukul singh
?>

Javascript


输出:
*
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***