打印卍图案镜像的程序
给定行数和列数,打印出万字符图案的镜像。
注意:行数和列数应相同且为奇数。这将生成一个完美的卍图案镜像。
例子 :
Input : row = 5, col = 5
Output :
* * * *
* *
* * * * *
* *
* * * *
Input : row = 9, col = 9
Output :
* * * * * *
* *
* *
* *
* * * * * * * * *
* *
* *
* *
* * * * * *
下面是打印卐字符图案镜像的实现。
C++
// C++ implementation to print
// mirror image of swastika pattern.
#include
using namespace std;
// function to print mirror image of swastika
void revswastika(int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++) {
// for the upper half of the Swastika
if (i < row / 2) {
// checking if j < col/2
if (j < col / 2) {
// the first quadrant will have
// stars till half the number of rows
if (i == 0 && i < row / 2)
cout << "*" << " ";
// rest will have spaces
else
cout << " " << " ";
}
// the middle line of the swastika
else if (j == col / 2)
cout << "*";
else {
// the second quadrant of the
// upper half will have spaces
if (i < row / 2 && j < col - 1)
cout << " " << " ";
if (j == col - 1)
// only the last column of the
// second quadrant will have stars
cout << " " << "*";
}
}
// for the lower half of the Swastika
// the middle horizontal line of the
// Swastika will be at row/2
else if (i == row / 2)
cout << "*" << " ";
else
{
// for stars in the third quadrant
// and the middle line of the lower Swastika
if (j == col / 2 || j == 0)
cout << "*" << " ";
else if (i == row - 1)
{
// the last row's third quadrant
// will have spaces and the fourth
// will have stars
if (j <= col / 2 || j == col)
cout << " " << " ";
else
cout << "*" << " ";
}
// rest of the remaining portion
// will have spaces all along
else
cout << " " << " ";
}
}
cout << "\n";
}
}
// Driver code
int main()
{
// same no. of rows and columns to
// get the perfect mirror image
// of swastika pattern.
int row = 9, col = 9;
// function calling
revswastika(row, col);
return 0;
}
Java
// Java implementation to print
// mirror image of swastika pattern.
import java.io.*;
class GFG
{
// function to print mirror image of swastika
static void revswastika(int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
// for the upper half of the Swastika
if (i < row / 2)
{
// checking if j < col/2
if (j < col / 2)
{
// the first quadrant will have
// stars till half the number of rows
if (i == 0 && i < row / 2)
System.out.print("*" + " ");
// rest will have spaces
else
System.out.print(" " + " ");
}
// the middle line of the swastika
else if (j == col / 2)
System.out.print("*");
else
{
// the second quadrant of the
// upper half will have spaces
if (i < row / 2 && j < col - 1)
System.out.print(" " + " ");
if (j == col - 1)
// only the last column of the
// second quadrant will have stars
System.out.print(" " + "*");
}
}
// for the lower half of the Swastika
// the middle horizontal line of the
// Swastika will be at row/2
else if (i == row / 2)
System.out.print("*" + " ");
else
{
// for stars in the third quadrant
// and the middle line of the lower Swastika
if (j == col / 2 || j == 0)
System.out.print( "*" + " ");
else if (i == row - 1)
{
// the last row's third quadrant
// will have spaces and the fourth
// will have stars
if (j <= col / 2 || j == col)
System.out.print (" " + " ");
else
System.out.print("*" + " ");
}
// rest of the remaining portion
// will have spaces all along
else
System.out.print(" " + " ");
}
}
System.out.println();
}
}
// Driver code
public static void main (String[] args)
{
// same no. of rows and columns to
// get the perfect mirror image
// of swastika pattern.
int row = 9, col = 9;
// function calling
revswastika(row, col);
}
}
// This code is contributed by vt_m.
Python3
# Python3 implementation to print
# mirror image of swastika pattern.
# Function to print mirror image of swastika
def revswastika(row, col):
for i in range(row):
for j in range(col):
# for the upper half of the Swastika
if (i < row // 2):
# checking if j < col/2
if (j < col // 2):
# The first quadrant will have
# stars till half the number of rows
if (i == 0 and i < row // 2):
print("* ", end = "")
# rest will have spaces
else:
print(" ", end = " ")
# the middle line of the swastika
elif (j == col // 2):
print("*", end = "")
else:
# the second quadrant of the
# upper half will have spaces
if (i < row // 2 and j < col - 1):
print(" ", end = " ")
if (j == col - 1):
# only the last column of the
# second quadrant will have stars
print(" *", end = "")
# For the lower half of the Swastika
# the middle horizontal line of the
# Swastika will be at row/2
elif (i == row // 2):
print("* ", end = "")
else:
# For stars in the third quadrant and
# the middle line of the lower Swastika
if (j == col // 2 or j == 0):
print("* ", end = "")
elif (i == row - 1):
# last row's third quadrant will
# have spaces and fourth will have stars
if (j <= col // 2 or j == col):
print(" ", end = " ")
else:
print("* ", end = "")
# rest of the remaining portion
# will have spaces all along
else:
print(" ", end = " ")
print()
# Driver code
# same no. of rows and columns to
# get the perfect mirror image
# of swastika pattern.
row = 9; col = 9
revswastika(row, col)
# This code is contributed by Azkia Anam.
C#
// C# implementation to print mirror
// image of swastika pattern.
using System;
class GFG {
// function to print mirror image
// of swastika
static void revswastika(int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
// for the upper half of the
// Swastika
if (i < row / 2)
{
// checking if j < col/2
if (j < col / 2)
{
// the first quadrant
// will have stars till
// half the number of
// rows
if (i == 0 && i < row / 2)
Console.Write("*" + " ");
// rest will have spaces
else
Console.Write(" " + " ");
}
// the middle line of the
// swastika
else if (j == col / 2)
Console.Write("*");
else
{
// the second quadrant of
// the upper half will
// have spaces
if (i < row / 2 && j < col - 1)
Console.Write(" " + " ");
if (j == col - 1)
// only the last column
// of the second quadrant
// will have stars
Console.Write(" " + "*");
}
}
// for the lower half of the Swastika
// the middle horizontal line of the
// Swastika will be at row/2
else if (i == row / 2)
Console.Write("*" + " ");
else
{
// for stars in the third quadrant
// and the middle line of the
// lower Swastika
if (j == col / 2 || j == 0)
Console.Write( "*" + " ");
else if (i == row - 1)
{
// the last row's third quadrant
// will have spaces and the fourth
// will have stars
if (j <= col / 2 || j == col)
Console.Write(" " + " ");
else
Console.Write("*" + " ");
}
// rest of the remaining portion
// will have spaces all along
else
Console.Write(" " + " ");
}
}
Console.WriteLine();
}
}
// Driver code
public static void Main ()
{
// same no. of rows and columns to
// get the perfect mirror image
// of swastika pattern.
int row = 9, col = 9;
// function calling
revswastika(row, col);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
* * * * * *
* *
* *
* *
* * * * * * * * *
* *
* *
* *
* * * * * *