编写一个C++程序,使用for循环打印倒置的V型星形图案或方形星形图案内的半个钻石。
#include 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";
}
#include 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 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 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
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$ $$$$$$$
$$$$$$ $$$$$$
$$$$$ $$$$$
$$$$ $$$$
$$$ $$$
$$ $$
$ $