使用 for 循环编写一个 C++ 程序,在方形图案中打印空心菱形星形图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, l, rows;
cout << "Enter Hollow Diamond Star Pattern Row = ";
cin >> rows;
cout << "Hollow Diamond Star Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = i; j <= rows; j++)
{
cout << "*";
}
for(k = 1; k <= i * 2 - 2; k++)
{
cout << " ";
}
for(l = i; l <= rows; l++)
{
cout << "*";
}
cout << "\n";
}
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << "*";
}
for (k = 2 * i - 2 ; k < 2 * rows - 2; k++ )
{
cout << " ";
}
for(l = 1; l <= i; l++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}

这个C++ 示例在方形图案中打印给定字符的空心菱形。
#include<iostream>
using namespace std;
int main()
{
int i = 1, j, k, l, rows;
char ch;
cout << "Enter Row = ";
cin >> rows;
cout << "Enter Symbol for Hollow Diamond Pattern = ";
cin >> ch;
while(i <= rows)
{
j = i;
while(j <= rows)
{
cout << ch;
j++;
}
k = 1;
while( k <= i * 2 - 2)
{
cout << " ";
k++;
}
l = i;
while( l <= rows)
{
cout << ch;
l++;
}
cout << "\n";
i++;
}
i = 1;
while( i <= rows)
{
j = 1;
while(j <= i)
{
cout << ch;
j++;
}
k = 2 * i - 2 ;
while ( k < 2 * rows - 2)
{
cout << " ";
k++;
}
l = 1;
while( l <= i)
{
cout << ch;
l++;
}
cout << "\n";
i++;
}
return 0;
}
Enter Row = 12
Enter Symbol for Hollow Diamond Pattern = @
@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@ @@@@@@@@@@@
@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@ @@@@@@@@
@@@@@@@ @@@@@@@
@@@@@@ @@@@@@
@@@@@ @@@@@
@@@@ @@@@
@@@ @@@
@@ @@
@ @
@ @
@@ @@
@@@ @@@
@@@@ @@@@
@@@@@ @@@@@
@@@@@@ @@@@@@
@@@@@@@ @@@@@@@
@@@@@@@@ @@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@ @@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@