打印镜像半钻星图案的C++程序

120 阅读1分钟

编写一个C++程序,使用for循环打印镜像的半钻石星形图案。

#include using namespace std;

int main() { int i, j, k, rows;

cout << "Enter Mirrored Half Diamond Star Pattern Row = ";
cin >> rows;

cout << "Mirrored Half Diamond Star Pattern\\n"; 

for(i = 1; i <= rows; i++)
{
	for(j = 1; j <= rows - i; j++)
	{
        cout << " ";
    }
    for(k = 1; k <= i; k++)
	{
        cout << "\*";
    }
    cout << "\\n";
}	

for(i = rows - 1; i > 0; i--)
{
	for(j = 1; j <= rows - i; j++)
	{
        cout << " ";
    }
    for(k = 1; k <= i; k++)
	{
        cout << "\*";
    }
    cout << "\\n";
}	
return 0;

image.png

这个C++例子使用while循环打印一个给定字符的镜像半钻石图案。

#include using namespace std;

int main() { int i = 1, j, k, rows; char ch;

cout << "Enter Mirrored Half Diamond Star Pattern Row = ";
cin >> rows;

cout << "Enter Symbol Mirrored Half Diamond Pattern = ";
cin >> ch;

cout << "Mirrored Half Diamond Star Pattern\\n"; 

while(i <= rows)
{
    j = 1;
	while( j <= rows - i)
	{
        cout << " ";
        j++;
    }
    k = 1;
    while( k <= i)
	{
        cout << ch;
        k++;
    }
    cout << "\\n";
    i++;
}	

i = rows - 1;
while( i > 0)
{
	j = 1;
	while( j <= rows - i)
	{
        cout << " ";
        j++;
    }
    k = 1;
    while( k <= i)
	{
        cout << ch;
        k++;
    }
    cout << "\\n";
    i--;
}	
return 0;
Enter Mirrored Half Diamond Star Pattern Row = 9
Enter Symbol Mirrored Half Diamond Pattern = $
Mirrored Half Diamond Star Pattern
        $
       $$
      $$$
     $$$$
    $$$$$
   $$$$$$
  $$$$$$$
 $$$$$$$$
$$$$$$$$$
 $$$$$$$$
  $$$$$$$
   $$$$$$
    $$$$$
     $$$$
      $$$
       $$
        $