打印皇冠图案的程序
给定长度值,使用“*”和“#”打印皇冠图案。
注意:为了打印一个完美的皇冠,长度的值取大于 15 的奇数。
例子 :
Input: length = 19
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
###################
###################
###################
*******************
Input: length = 25
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
##### ######### #####
###### ########### ######
#########################
#########################
#########################
#########################
*************************
下面是打印皇冠图案的实现:
C++
// C++ implementation to print
// Crown pattern
#include
using namespace std;
// function to print crown pattern
void crown(int length, int height)
{
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
// for first row, print '*'
// i.e, for top part of crown
if (i == 0) {
// print '*' at first, middle and last column
if (j == 0 || j == height || j == length - 1) {
cout << "*";
}
else {
cout << " ";
}
}
// print '*' at base of
// crown i.e, for last row
else if (i == height - 1) {
cout << "*";
}
// fill '#' to make a perfect crown
else if ((j < i || j > height - i) &&
(j < height + i || j >= length - i))
cout << "#";
else
cout << " ";
}
cout << "\n";
}
}
// Driver code
int main()
{
// length of crown
int length = 25;
// height of crown
int height = (length - 1) / 2;
// function calling
crown(length, height);
return 0;
}
Java
// Java implementation to print
// Crown pattern
import java.io.*;
class GFG
{
// function to print crown pattern
static void crown(int length, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < length; j++)
{
// for first row, print '*'
// i.e, for top part of crown
if (i == 0)
{
// print '*' at first, middle and last column
if (j == 0 || j == height || j == length - 1)
{
System.out.print("*");
}
else {
System.out.print(" ");
}
}
// print '*' at base of
// crown i.e, for last row
else if (i == height - 1)
{
System.out.print("*");
}
// fill '#' to make a perfect crown
else if ((j < i || j > height - i) &&
(j < height + i || j >= length - i))
System.out.print("#");
else
System.out.print(" ");
}
System.out.println();
}
}
// Driver code
public static void main (String[] args)
{
// length of crown
int length = 25;
// height of crown
int height = (length - 1) / 2;
// function calling
crown(length, height);
}
}
// This code is contributed by vt_m.
Python3
# Python implementation to
# print Crown pattern
# Function to print
# crown pattern
def crown(length, height) :
for i in range(0, height) :
for j in range(0, length) :
# for first row, print '*'
# i.e, for top part of crown
if (i == 0) :
# print '*' at first,
# middle and last column
if (j == 0 or j == height or
j == length - 1) :
print ("*", end = "")
else :
print (" ", end = "")
# print '*' at base of
# crown i.e, for last row
elif (i == height - 1) :
print ("*", end = "")
# fill '#' to make
# a perfect crown
elif ((j < i or j > height - i) and
(j < height + i or
j >= length - i)) :
print ("*", end = "")
else :
print (" ", end = "")
print ()
# Driver code
# length of crown
length = 25
# height of crown
height = int((length - 1) / 2)
# function calling
crown(length, height)
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# implementation to print
// Crown pattern
using System;
class GFG {
// function to print crown pattern
static void crown(int length, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < length; j++)
{
// for first row, print '*'
// i.e, for top part of crown
if (i == 0)
{
// print '*' at first, middle
// and last column
if (j == 0 || j == height
|| j == length - 1)
{
Console.Write("*");
}
else {
Console.Write(" ");
}
}
// print '*' at base of
// crown i.e, for last row
else if (i == height - 1)
{
Console.Write("*");
}
// fill '#' to make a perfect crown
else if ((j < i || j > height - i) &&
(j < height + i ||
j >= length - i))
Console.Write("#");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
// Driver code
public static void Main ()
{
// length of crown
int length = 25;
// height of crown
int height = (length - 1) / 2;
// function calling
crown(length, height);
}
}
// This code is contributed by vt_m.
PHP
$height - $i) &&
($j < $height + $i ||
$j >= $length - $i))
echo "#";
else
echo " ";
}
echo "\n";
}
}
// Driver code
// length of crown
$length = 25;
// height of crown
$height = ($length - 1) / 2;
// function calling
crown($length, $height);
// This code is contributed by mits.
?>
Javascript
输出:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
##### ######### #####
###### ########### ######
#########################
#########################
#########################
#########################
*************************