打印实心和空心菱形图案的程序
对于任何给定的数字 n,打印用星星 (*) 制成的空心和实心正方形和菱形。
例子:
Input : n = 4
Output :
Solid Rhombus:
****
****
****
****
Hollow Rhombus:
****
* *
* *
****
1. 实心菱形:制作实心菱形有点类似于制作实心正方形,而不是每个第 i 行我们在星号前有 ni 个空格的概念:
– – – ****
– – ****
– ****
****
2. 空心菱形:空心菱形的形成使用了空心方形和实心菱形形成背后的思想。空心正方形在每第 i 行中的星星前有 ni 个空格导致形成空心菱形。
C/C++
C++
// C++ program to print
// hollow and solid rhombus patterns
#include
using namespace std;
// Function for Solid Rhombus
void solidRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
cout << " ";
// Print stars after spaces
for (j=1; j<=rows; j++)
cout << "*";
// Move to the next line/row
cout << "\n";
}
}
// Function for Rhombus
void hollowRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
cout << " ";
// Print stars after spaces
// Print stars for each solid rows
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
cout << "*";
// stars for hollow rows
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
cout << "*";
else
cout << " ";
// Move to the next line/row
cout << "\n";
}
}
// utility program to print all patterns
void printPattern(int rows)
{
cout << "\nSolid Rhombus:\n";
solidRhombus(rows);
cout << "\nHollow Rhombus:\n";
hollowRhombus(rows);
}
// driver program
int main()
{
int rows = 5;
printPattern (rows);
return 0;
}
Java
// Java program to print
// hollow and solid rhombus patterns
import java.io.*;
class GFG
{
// Function for Solid Rhombus
static void solidRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
System.out.print(" ");
// Print stars after spaces
for (j=1; j<=rows; j++)
System.out.print("*");
// Move to the next line/row
System.out.println();
}
}
// Function for Hollow Rhombus
static void hollowRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
System.out.print(" ");
// Print stars after spaces
// Print stars for each solid rows
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
System.out.print("*");
// stars for hollow rows
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
System.out.print("*");
else
System.out.print(" ");
// Move to the next line/row
System.out.println();
}
}
// utility program to print all patterns
static void printPattern(int rows)
{
System.out.println("Solid Rhombus:");
solidRhombus(rows);
System.out.println("Hollow Rhombus:");
hollowRhombus(rows);
}
// driver program
public static void main (String[] args)
{
int rows = 5;
printPattern (rows);
}
}
// Contributed by Pramod Kumar
Python 3
# Python 3 program to print
# hollow and solid rhombus patterns
# Function for Solid Rhombus
def solidRhombus(rows):
for i in range (1,rows + 1):
# Print trailing spaces
for j in range (1,rows - i + 1):
print (end=" ")
# Print stars after spaces
for j in range (1,rows + 1):
print ("*",end="")
# Move to the next line/row
print()
# Function for Hollow Rhombus
def hollowRhombus(rows):
for i in range (1, rows + 1):
# Print trailing spaces
for j in range (1, rows - i + 1):
print (end=" ")
# Print stars after spaces
# Print stars for each solid rows
if i == 1 or i == rows:
for j in range (1, rows + 1):
print ("*",end="")
# stars for hollow rows
else:
for j in range (1,rows+1):
if (j == 1 or j == rows):
print ("*",end="")
else:
print (end=" ")
# Move to the next line/row
print()
# utility program to print all patterns
def printPattern(rows):
print ("Solid Rhombus:")
solidRhombus(rows)
print("\nHollow Rhombus:")
hollowRhombus(rows)
# driver program
if __name__ == "__main__":
rows = 5
printPattern (rows)
C#
// C# program to print
// hollow and solid rhombus patterns
using System;
class GFG
{
// Function for Solid Rhombus
static void solidRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
Console.Write(" ");
// Print stars after spaces
for (j=1; j<=rows; j++)
Console.Write("*");
// Move to the next line/row
Console.WriteLine();
}
}
// Function for Hollow Rhombus
static void hollowRhombus(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print trailing spaces
for (j=1; j<=rows - i; j++)
Console.Write(" ");
// Print stars after spaces
// Print stars for each solid rows
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
Console.Write("*");
// stars for hollow rows
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
Console.Write("*");
else
Console.Write(" ");
// Move to the next line/row
Console.WriteLine();
}
}
// utility program to print all patterns
static void printPattern(int rows)
{
Console.WriteLine("\nSolid Rhombus:\n");
solidRhombus(rows);
Console.WriteLine("\nHollow Rhombus:\n");
hollowRhombus(rows);
}
// driver program
public static void Main () {
int rows = 5;
printPattern (rows);
}
}
// Contributed by vt_m.
PHP
Javascript
输出:
Solid Rhombus:
*****
*****
*****
*****
*****
Hollow Rhombus:
*****
* *
* *
* *
*****