📜  程序打印字样

📅  最后修改于: 2021-06-01 00:22:28             🧑  作者: Mango

给定行数和列数,使用循环打印相应的十字记号图案。
注意:行数和列数应相同且为奇数。这将生成一个完美的十字形图案。
例子 :

Input : row = 7, column = 7        
Output:
*     * * * *
*     *
*     *
* * * * * * *
      *     *
      *     *
* * * *     *

Input : row = 11, column = 11
Output :
*         * * * * * *
*         *
*         *
*         *
*         *
* * * * * * * * * * * 
          *         * 
          *         * 
          *         * 
          *         * 
* * * * * *         * 

推荐:在继续解决方案之前,请先在{IDE}上尝试您的方法
以下是打印十字记号图案的实现。

C++
// C++ implementation to
// print swastika pattern
#include 
using namespace std;
 
// function to print swastika
void swastika(int row, int col)
{
for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
         
    // checking if i < row/2
    if (i < row / 2) {
         
        // checking if j row/2
        if (j == col / 2 || j == col - 1)
        cout << "* ";
         
        // last row
        else if (i == row - 1) {
             
        // last row will be have '*' if
        // j <= col/2 or if it is last column
        if (j <= col / 2 || j == col - 1)
            cout << "* ";
        else
            cout << " " << " ";
        }
        else
        cout << " " << " ";
    }
    }
    cout << "\n";
}
}
 
// driver code
int main()
{
   // odd number of row and column
   // to get perfect swastika
   int row = 7, col = 7;
    
   // function calling
   swastika(row, col);
    
   return 0;
}


Java
// Java implementation to
// print swastika pattern
 
class GFG
{
    // function to print swastika
    static void swastika(int row, int col)
    {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++)
            {
                // checking if i < row/2
                if (i < row / 2)
                {
                     
                    // checking if j row/2
                    if (j == col / 2 || j == col - 1)
                    System.out.print("* ");
                     
                    // last row
                    else if (i == row - 1)
                    {
                         
                        // last row will be have '*' if
                        // j <= col/2 or if it is last column
                        if (j <= col / 2 || j == col - 1)
                            System.out.print("* ");
                        else
                            System.out.print(" "+ " ");
                    }
                    else
                    System.out.print(" "+" ");
                }
            }
            System.out.print("\n");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        // odd number of row and column
        // to get perfect swastika
        int row = 7, col = 7;
         
        // function calling
        swastika(row, col);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to print swastika pattern
 
# Function to print swastika
def swastika(row,col):
     
    for i in range(row):
        for j in range(col):
             
            # checking if i < row/2
            if(i < row // 2):
                 
                # checking if j row/2
                if (j == col // 2 or j == col - 1):
                    print("* ", end = "")
                     
                # last row
                elif (i == row - 1):
                     
                    # last row will be have '*' if
                    # j <= col/2 or if it is last column
                    if (j <= col // 2 or j == col - 1):
                        print("* ", end = "")
                    else:
                        print(" ", end = " ")
                else:
                    print(" ", end = " ")
        print()
         
# Driver code
 
# odd number of row and column
# to get perfect swastika
row = 7; col = 7
 
# Function calling
swastika(row, col)
 
# This code is contributed by Azkia Anam.


C#
// C# implementation to print swastika pattern
using System;
 
class GFG {
     
    // function to print swastika
    static void swastika(int row, int col)
    {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++)
            {
                 
                // checking if i < row/2
                if (i < row / 2)
                {
                     
                    // checking if j < col/2
                    if (j < col / 2)
                    {
                         
                        // print '*' if j = 0
                        if (j == 0)
                            Console.Write("*");
                             
                        // else print space
                        else
                            Console.Write(" "+ " ");
                    }
                     
                    // check if j = col/2
                    else if (j == col / 2)
                        Console.Write(" *");
                     
                    else
                    {
                        // if i=0 then first row
                        // will have '*'
                        if (i == 0)
                            Console.Write(" *");
                    }
                }
                else if (i == row / 2)
                    Console.Write("* ");
                else
                {
                     
                    // middle column and last column
                    // will have '*' after i > row/2
                    if (j == col / 2 || j == col - 1)
                        Console.Write("* ");
                     
                    // last row
                    else if (i == row - 1)
                    {
                         
                        // last row will be have '*' if
                        // j <= col/2 or if it is last column
                        if (j <= col / 2 || j == col - 1)
                            Console.Write("* ");
                        else
                            Console.Write(" "+ " ");
                    }
                    else
                        Console.Write(" "+" ");
                }
            }
             
            Console.WriteLine();
        }
    }
     
    // Driver code
    public static void Main ()
    {
         
        // odd number of row and column
        // to get perfect swastika
        int row = 7, col = 7;
         
        // function calling
        swastika(row, col);
    }
}
 
// This code is contributed by vt_m.


PHP
 row/2
        if ($j == floor($col / 2 )||
            $j == $col - 1)
        echo "* ";
         
        // last row
        else if ($i == $row - 1)
        {
             
        // last row will be have
        // '*' if j <= col/2 or
        // if it is last column
        if ($j <= floor($col / 2) ||
            $j == $col - 1)
            echo "* ";
        else
            echo " " . " ";
        }
        else
        echo " " . " ";
    }
    }
    echo "\n";
}
}
 
// Driver Code
 
// odd number of row
// and column to get
// perfect swastika
$row = 7;
$col = 7;
     
// function calling
swastika($row, $col);
     
// This code is contributed by ajit
?>


Javascript


输出:

*     * * * *
*     *
*     *
* * * * * * *
      *     *
      *     *
* * * *     *
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”