三角形图案居中
我想在 C++ 中创建一个看起来像三角形(或半个菱形)的图案 使用星号:模式应该有 1、2、3、4,并以 5 颗星结尾,如下所示
*
**
***
****
*****
(但是直的!) 我的代码如下:
-`#include 使用命名空间 std; int main() {
int size;
cout<<"size:"<<endl;
cin>>size;
int blank=size/2;
int newsize=1;
for (int i=0; i<=size/2; i++)
{
for(int j=blank;j>0;j--)
cout <<" ";
blank--;
for(int j=newsize; j>0; j--)
cout <<"*";
newsize+=2;
cout <<endl;
}
return 0;
} ` 我唯一的问题是它像这样显示 1、3 和 5 颗星。
*
***
*****
这只是一个小问题,但尽管我更改了代码的不同部分 我似乎不太明白。
有什么建议吗?
谢谢 :)
i want to create a pattern in c++ that looks like a trianlge(or half a diamond)
using asteriscks: the pattern should have 1, 2, 3, 4, and end in 5 stars like this
*
**
***
****
*****
(but straight!)
my code is as follows:
-`#include
using namespace std;
int main()
{
int size;
cout<<"size:"<<endl;
cin>>size;
int blank=size/2;
int newsize=1;
for (int i=0; i<=size/2; i++)
{
for(int j=blank;j>0;j--)
cout <<" ";
blank--;
for(int j=newsize; j>0; j--)
cout <<"*";
newsize+=2;
cout <<endl;
}
return 0;
}
`
my only problem with it is that it displays 1, 3,and 5 stars like this.
*
***
*****
its just a minor problem but although i have changed different parts of the code
i dont seem to get it right.
any suggestions?
thanks
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定你所说的“但是直接”是什么意思,所以我现在就忽略它......
从
blank
与size
相同的值开始,所以您可以每次递减该值,而不必递减一半:循环到
size
而不是size/2
以获得更多行:在循环中递减 2对于空格以获得一半的空格数:
将大小增加一而不是二以获得较慢的增加:
这应该会产生您显示的输出。
编辑:
我对此进行了确定,输出是:
要获得您要求的确切输出,请从空白开始少一个:
I'm not sure what you mean by "but straight", so I'll just ignore that for now...
Start with
blank
the same value assize
, so that you can decrement the value each time without having to decrement by a half:Loop up to
size
instead ofsize/2
to get more lines:Decrement by two in the loop for spaces to get half the number of spaces:
Increase the size by one instead of two to get the slower increase:
That should produce the output that you showed.
Edit:
I tested this to be sure, and the output is:
To get the exact output that you asked for, start with blank one less:
我做对了吗:你想在字符位置的边界上放置一些星号?如果是这样的话,那是不可能的。当以等宽字体显示时,每个星号(或任何其他符号)将位于字符位置的中间,就像在网格中一样。您可以在单元格内放置星号,但不能将星号放置在网格的边框上。
Did I get it right: you want to place some asterisks on borders of character places? If so, it isn't possible. Every asterisk (or any other symbol), when displayed in monospace fonts, will reside in a middle of a character place, like in a grid. You can place asterisks inside the cells, but you cannot place asterisks on the borders of the grid.