打印皇冠图案的程序

给定长度值,使用“*”和“#”打印皇冠图案。 注: 为了打印出完美的皇冠,请将长度值设为大于15的奇数。 例如:

null
Input: length = 19Output:*        *        *#        #        ###      ###      #####    #####    #######  #######  #############################################################*******************Input: length = 25Output:*           *           *#           #           ###         ###         #####       #####       #######     #######     #########   #########   ########### ########### ##########################################################################################################*************************

下面是打印皇冠图案的实现:

C++

// C++ implementation to print
// Crown pattern
#include <bits/stdc++.h>
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 << "" ;
}
}
// 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

<?php
// PHP implementation to print
// Crown pattern
// Function to print crown pattern
function crown( $length , $height )
{
for ( $i = 0; $i < $height ; $i ++)
{
for ( $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)
{
echo "*" ;
}
else
{
echo " " ;
}
}
// print '*' at base of
// crown i.e, for last row
else if ( $i == $height - 1)
{
echo "*" ;
}
// fill '#' to make a perfect crown
else if (( $j < $i ||
$j > $height - $i ) &&
( $j < $height + $i ||
$j >= $length - $i ))
echo "#" ;
else
echo " " ;
}
echo "" ;
}
}
// 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

<script>
// JavaScript implementation to print
// Crown pattern
// function to print crown pattern
function crown(length, height) {
for ( var i = 0; i < height; i++) {
for ( var 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) {
document.write( "*" );
} else {
document.write( "  " );
}
}
// print '*' at base of
// crown i.e, for last row
else if (i == height - 1) {
document.write( "*" );
}
// fill '#' to make a perfect crown
else if (
(j < i || j > height - i) &&
(j < height + i || j >= length - i)
)
document.write( "#" );
else document.write( "  " );
}
document.write( "<br>" );
}
}
// Driver code
// length of crown
var length = 25;
// height of crown
var height = parseInt((length - 1) / 2);
// function calling
crown(length, height);
</script>


输出:

*           *           *#           #           ###         ###         #####       #####       #######     #######     #########   #########   ########### ########### ##########################################################################################################*************************

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞15 分享