📜  程序在方格内打印方格

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

给定数字N,请打印一个带有N个星号(’*’)的空心正方形,并在其中打印一个带有N-4个星号(’*’)的空心正方形。

例子 :

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

Input :9
Output :
*********
*       *
* ***** *
* *   * *
* *   * *
* *   * *
* ***** *
*       *
*********

这个想法是为行和列运行两个从1到n的嵌套循环,现在检查何时打印星号(’*’)和何时打印空格(”)的条件。打印星号的条件为:

// This will print asterisks on the boundary
(i == 1 || i == n || j == 1 || j == n)

// This will print the asterisks on the boundary
// of inner square
(i >= 3 && i = 3 && j <= n - 2) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2))

不满足上述条件的所有索引将包含空格。

下面是上述想法的实现:

C
// C program to print square inside
// a square pattern
#include 
  
// Function to print the pattern square 
// inside a square
void printPattern(int n) 
{
    int i, j;
      
    // To access rows of the square
    for (i = 1; i <= n; i++) 
    {
        // To access columns of the square
        for (j = 1; j <= n; j++)
        {
      
            // For printing the required square pattern 
            if ((i == 1 || i == n || j == 1 || j == n) ||
                (i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2) &&
                 (i == 3 || i == n - 2 || j == 3 || j == n - 2))
            {
                // Prints star(*)
                printf("*"); 
            } 
            else
            {
                // Prints space(" ")
                printf(" "); 
            }
        }
      
        printf("\n");
    }
}
  
// Driver Code
int main() 
{
    int n = 7; 
    printPattern(n); 
    return 0;
}


Java
// Java program to print square inside
// a square pattern
import java.lang.*;
  
class GFG {
      
// Function to print the pattern square
// inside a square
static void printPattern(int n) {
  
    // To access rows of the square
    for (int i = 1; i <= n; i++) {
          
    // To access columns of the square
    for (int j = 1; j <= n; j++) {
  
        /*** For printing the required square pattern ***/
        if ((i == 1 || i == n || j == 1 || j == n) ||
            (i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2) &&
                (i == 3 || i == n - 2 || j == 3 || j == n - 2)) 
        {
        // Prints star(*)            
        System.out.print("*"); 
        } 
        else
        {
        // Prints space(" ")    
        System.out.print(" "); 
        }
    }
  
    System.out.print("\n");
    }
}
  
// Driver code
public static void main(String[] args) {
    int n = 7;
    printPattern(n);
}
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python program to
# print square inside
# a square pattern
  
# Function to print
# the pattern square 
# inside a square
def printPattern(n):
      
    # To access rows of the square
    for i in range(1,n+1): 
      
        # To access columns of the square
        for j in range(1,n+1):
          
            # For printing the required square pattern 
            if ((i == 1 or i == n or j == 1 or j == n) or
                (i >= 3 and i <= n - 2 and j >= 3 and j <= n - 2) and
                    (i == 3 or i == n - 2 or j == 3 or j == n - 2)):
              
                print("*",end="") # Prints star(*)
              
            else:
              
                print(" ",end="") # Prints space(" ")
              
        print()
          
  
# Driver code
  
n = 7   
printPattern(n)
  
# This code is contributed
# by Anant Agarwal.


C#
// C# program to print square inside
// a square pattern
using System;
  
class GFG {
      
    // Function to print the pattern square
    // inside a square
    static void printPattern(int n) {
      
        // To access rows of the square
        for (int i = 1; i <= n; i++) {
              
            // To access columns of the square
            for (int j = 1; j <= n; j++) {
          
                /*** For printing the required 
                     square pattern ***/
                if ((i == 1 || i == n || j == 1 
                       || j == n) || (i >= 3 && 
                        i <= n - 2 && j >= 3 && 
                      j <= n - 2) && (i == 3 || 
                        i == n - 2 || j == 3 ||
                                   j == n - 2)) 
                {
                      
                    // Prints star(*)     
                    Console.Write("*"); 
                } 
                else
                {
                      
                    // Prints space(" ") 
                    Console.Write(" "); 
                }
            }
          
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main() {
          
        int n = 7;
          
        printPattern(n);
    }
}
  
// This code is contributed by vt_m.


PHP
= 3 && $i <= $n - 2 && 
                $j >= 3 && $j <= $n - 2) &&
                ($i == 3 || $i == $n - 2 ||
                $j == 3 || $j == $n - 2))
            {
                // Prints star(*)
                echo "*"; 
            } 
            else
            {
                // Prints space " "
                echo " "; 
            }
        }
      
        echo"\n";
    }
}
  
// Driver Code
$n = 7; 
printPattern($n);
  
// This code is contributed by Mithun Kumar
?>


输出 :

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

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