📜  打印窗口图案的程序

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

打印窗口图案的程序

打印其中有一个空心正方形和加号的图案。该模式将根据示例中给出的 n 即行数。

例子:

Input : 6
Output : * * * * * *
         *   * *   * 
         * * * * * * 
         * * * * * * 
         *   * *   * 
         * * * * * *

Input : 7
Output : * * * * * * * 
         *     *     * 
         *     *     * 
         * * * * * * * 
         *     *     * 
         *     *     * 
         * * * * * * *

方法:

  • 我们将开始一个 for 循环直到 n 并且在其中也有 for 循环直到 n。
  • 在这里,我们只需检查行是第一个还是最后一个或列是第一个还是最后一个,然后打印“*”。
  • 现在我们必须检查中间的行和列。
  • 因此,当 n 为奇数时,我们将有一个中间的行和列,如果行或列在中间,那么我们将打印“*”。
  • 如果 n 是偶数,那么行或列如果等于这些值 n/2 和 (n/2)+1,那么我们将打印“*”。
  • 其他地方我们必须打印“”(空格)。

下面是实现。

C++14
// C++ program to print the pattern 
// hollow square with plus inside it
// window pattern
#include 
using namespace std;
  
// Function to print pattern n means 
// number of rows which we want
void window_pattern (int n)
{
    int c, d;
      
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
        c = (n / 2) + 1;
        d = 0;
    }
  
    // If n is even then we will have two
    // values
    else
    {
        c = (n / 2) + 1;
        d = n / 2 ;
    }
  
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
           
            // If i,j equals to corner row or
            // column then "*"
            if (i == 1 || j == 1 ||
                i == n || j == n)
                cout << "* ";
  
            else
            {
               
                // If i,j equals to the middle 
                // row or column then  "*"
                if (i == c || j == c)
                    cout << "* ";
  
                else if (i == d || j == d)
                    cout << "* ";
  
                else
                    cout << "  ";
            }
        }
        cout << '\n';
    }
}
  
// Driver Code
int main()
{
    int n = 7;
   
    window_pattern(n);
    return 0;   
}
  
// This code is contributed by himanshu77


Java
// Java program to print the pattern 
// hollow square with plus inside it
// window pattern
class GFG
{
 
  // Function to print pattern n means 
  // number of rows which we want
  static void window_pattern (int n)
  {
    int c, d;
 
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
      c = (n / 2) + 1;
      d = 0;
    }
 
    // If n is even then we will have
    // two values
    else
    {
      c = (n / 2) + 1;
      d = n / 2 ;
    } 
    for(int i = 1; i <= n; i++)
    {
      for(int j = 1; j <= n; j++)
      {
 
        // If i,j equals to corner row
        // or column then "*"
        if (i == 1 || j == 1 ||
            i == n || j == n)
          System.out.print("* ");          
        else
        {
 
          // If i,j equals to the middle 
          // row or column then  "*"
          if (i == c || j == c)
            System.out.print("* ");
          else if (i == d || j == d)
            System.out.print("* ");
          else
            System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int n = 7;
    window_pattern(n);
  }
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to print the pattern
# hollow square with plus inside it
# window pattern
 
 
# function to print pattern n means
# number of rows which we want
def window_pattern(n):
     
    # if n is odd then we will have
    # only one middle element
    if n % 2 != 0:
        c = ( n // 2 ) + 1
        d = 0
         
    # if n is even then we will have two
    # values
    else:
        c = ( n // 2 ) + 1
        d = ( n // 2 )
 
    for i in range( 1 , n + 1 ):
        for j in range( 1 , n + 1 ):
             
            # if i,j equals to corner row or
            # column then "*"
            if i == 1 or j == 1 or i == n or j == n:
                print("*",end=" ")
                 
            else:
                 
                # if i,j equals to the middle row
                # or column then  "*"
                if i == c or j == c:
                    print("*",end=" ")
                     
                elif i == d or j == d:
                    print("*",end=" ")
                 
                else:
                    print(" ",end=" ")
         
        print()
 
 
# Driver Code
if __name__ == "__main__":
    n = 7
    window_pattern(n)


C#
// C# program to print the pattern 
// hollow square with plus inside it
// window pattern
using System;
 
class GFG{
 
// Function to print pattern n means 
// number of rows which we want
static void window_pattern (int n)
{
    int c, d;
     
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
        c = (n / 2) + 1;
        d = 0;
    }
   
    // If n is even then we will have
    // two values
    else
    {
        c = (n / 2) + 1;
        d = n / 2 ;
    }
   
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
             
            // If i,j equals to corner row
            // or column then "*"
            if (i == 1 || j == 1 ||
                i == n || j == n)
                Console.Write("* ");
                 
            else
            {
                 
                // If i,j equals to the middle 
                // row or column then  "*"
                if (i == c || j == c)
                    Console.Write("* ");
   
                else if (i == d || j == d)
                    Console.Write("* ");
   
                else
                    Console.Write("  ");
            }
        }
        Console.WriteLine();
    }
}
 
// Driver code
static void Main()
{
    int n = 7;
     
    window_pattern(n);
}
}
 
// This code is contributed by divyesh072019


Javascript


输出 :

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

时间复杂度: O(n 2 )