#include <iostream>
using namespace std;
int main() {
// your code goes here
for(int j=1;j<= 4;j++)
{
for(int i=1;i<=8;i++)
{if (i<=j)
cout<<"* ";
else if (i+j >= 9)
cout<<"* ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
2 回答
1
这并不能直接回答问题,
但这是上述示例的 2-liner ( Live ):
for (auto k : { 1, 2, 3, 4 })
cout << string(k, '*') << string(8 - 2 * k, ' ') << string(k, '*') << endl;
于 2021-01-14T13:35:29.980 回答
0
好的,所以当我删除要在星号之后打印的额外空格(第 7 行)时,代码可以正常工作,但仍然可以正常使用空格!
于 2021-01-14T12:48:11.883 回答