创建用户在 C++ 中定义的星星对角线
Creating a diagonal line of stars that the user has defined in C++
我不得不编写一些需要创建星号对角线的代码。我知道如何编写输出三角形的代码:
cout<<"What size of stars would you like to draw? ";
cin>>star;
int space;
for(int i = 1, k = 0; i <= star; I++, k = 0)
{
for(space = 1; space <= star-i; space++)
{
cout <<" ";
}
while(k != 2*i-1)
{
cout << "* ";
k++;
}
cout << endl;
我修改了代码以从中创建一个半三角形,但我似乎无法找到如何将它变成对角线。这是我的半三角形:
for(int i = 1, k = 0; i <= star; i++, k = 0) {
while(k != 2*i-1) {
cout << "* ";
k++;
}
cout << endl;
}
只要将对角线想象成一个半三角形,其中填充了空 space :-)。
for(int i = 1, k = 0; i <= star; i++, k = 0) {
while(k != 2*i-1) {
cout << " "; // print whitespace until the end of the row
k++;
}
cout << "*" << endl; // print a dot at the end
}
我不得不编写一些需要创建星号对角线的代码。我知道如何编写输出三角形的代码:
cout<<"What size of stars would you like to draw? ";
cin>>star;
int space;
for(int i = 1, k = 0; i <= star; I++, k = 0)
{
for(space = 1; space <= star-i; space++)
{
cout <<" ";
}
while(k != 2*i-1)
{
cout << "* ";
k++;
}
cout << endl;
我修改了代码以从中创建一个半三角形,但我似乎无法找到如何将它变成对角线。这是我的半三角形:
for(int i = 1, k = 0; i <= star; i++, k = 0) {
while(k != 2*i-1) {
cout << "* ";
k++;
}
cout << endl;
}
只要将对角线想象成一个半三角形,其中填充了空 space :-)。
for(int i = 1, k = 0; i <= star; i++, k = 0) {
while(k != 2*i-1) {
cout << " "; // print whitespace until the end of the row
k++;
}
cout << "*" << endl; // print a dot at the end
}