- 编写程序,在屏幕上打印一个蝴蝶结领结。程序接受蝴蝶结的高度h作为输入,h是一个大于或等于5的奇整数。输出的蝴蝶结有h行2h列,例如
Enter height: 5
* *
*** ***
**********
*** ***
* *
Enter height: 7
* *
*** ***
***** *****
**************
***** *****
*** ***
* *
怎么打出代码 在c语言中 用循环语句
Enter height: 5
* *
*** ***
**********
*** ***
* *
Enter height: 7
* *
*** ***
***** *****
**************
***** *****
*** ***
* *
怎么打出代码 在c语言中 用循环语句
#include<iostream>
using namespace std;
int main()
{
int h;
cout << "请输入蝴蝶结的高h(h大于等于5,且h为奇数):";
cin >> h;
if (h >= 5 && h % 2 == 1)
{
for (int a = 1; a <= (h + 1) / 2; ++a)
{
{
for (int i = 1; i <= 2 * a - 1; ++i)
cout << "*";
for (int j = 1; j <= 2 * h - 2 * (2 * a - 1); ++j)
cout << ' ';
for (int k = 1; k <= 2 * a - 1; ++k)
cout << "*";
cout << endl;
}
cout << endl;
}
for (int b = 1; b <= h / 2; ++b)
{
{
for (int x = 1; x <= h - 2 * b; ++x)
cout << "*";
for (int y = 1; y <= 2 * h - 2 * (h - 2 * b); ++y)
cout << ' ';
for (int z = 1; z <= h - 2 * b; ++z)
cout << "*";
cout << endl;
}
cout << endl;
}
}
else
cout << "错误!";
}
我是初学者,大佬们嘴下留情,而且这个是C++,希望有帮助