编写一个 C++ 程序,使用 for 循环打印倒 V 星形图案或方形星形图案内的半个菱形。
#include<iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter Inverted V Shape Star Pattern Rows = ";
cin >> rows;
cout << "Printing Inverted V Shape Star Pattern\n";
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << "*";
}
for (int k = 1; k <= 2 * (rows - i); k++)
{
cout << " ";
}
for (int l = 1; l <= i; l++)
{
cout << "*";
}
cout << "\n";
}
}

此 C++ 程序使用 while 循环打印星形的倒 V 图案。
#include<iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter Inverted V Shape Star Pattern Rows = ";
cin >> rows;
cout << "Inverted V Shape Star Pattern\n";
int i = rows;
while (i >= 1)
{
int j = 1;
while (j <= i)
{
cout << "*";
j++;
}
int k = 1;
while (k <= 2 * (rows - i))
{
cout << " ";
k++;
}
int l = 1;
while (l <= i)
{
cout << "*";
l++;
}
cout << "\n";
i--;
}
}
Enter Inverted V Shape Star Pattern Rows = 12
Inverted V Shape Star Pattern
************************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
使用 do while 循环打印倒 V 星形图案的 C++ 程序
#include<iostream>
using namespace std;
int main()
{
int i, rows;
cout << "Enter Inverted V Shape Star Pattern Rows = ";
cin >> rows;
cout << "Inverted V Shape Star Pattern\n";
i = rows;
do
{
int j = 1;
do
{
cout << "*";
} while (++j <= i);
int k = 1;
while (k <= 2 * (rows - i))
{
cout << " ";
k++;
}
int l = 1;
do
{
cout << "*";
} while (++l <= i);
cout << "\n";
} while (--i >= 1);
}
Enter Inverted V Shape Star Pattern Rows = 15
Inverted V Shape Star Pattern
******************************
************** **************
************* *************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
在此 C++ 模式 示例 中,InvertedVShapePatternFun 允许输入任何字符,并显示给定字符的倒 V 形。
#include<iostream>
using namespace std;
void InvertedVShapePatternFun(int rows, char ch)
{
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << ch;
}
for (int k = 1; k <= 2 * (rows - i); k++)
{
cout << " ";
}
for (int l = 1; l <= i; l++)
{
cout << ch;
}
cout << "\n";
}
}
int main()
{
int rows;
char ch;
cout << "Enter Character for Inverted V Pattern = ";
scanf("%c", &ch);
cout << "Enter Inverted V Shape Star Pattern Rows = ";
scanf("%d", &rows);
cout << "Inverted V Shape Star Pattern\n";
InvertedVShapePatternFun(rows, ch);
}
Enter Character for Inverted V Pattern = $
Enter Inverted V Shape Star Pattern Rows = 17
Inverted V Shape Star Pattern
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$ $$$$$$$
$$$$$$ $$$$$$
$$$$$ $$$$$
$$$$ $$$$
$$$ $$$
$$ $$
$ $