简体   繁体   English

我想知道如何使用循环在C ++中制作一个星号直角三角形

[英]I'd Like to know how to make an asterisk right triangle(S) in C++ using a loop

I want something like this: 我想要这样的东西:

****      ****
***        ***
**          **
*            *

I tried this: 我尝试了这个:

void roadBound() {

    for( int i = 1; i <= 10; i++ ){
        for( int j = 0; j < i; j++ ){
           cout << "*" ;
        }
        cout << endl;
    }
}

But it wasnt even close. 但它甚至没有关闭。 Please help 请帮忙

So far, your code produces this: 到目前为止,您的代码产生了以下结果:

*
**
***
****
*****
******
*******
********
*********
**********

Which looks like a pretty good right triangle, so I think you're on the right track. 看起来像一个不错的直角三角形,所以我认为您在正确的轨道上。 To make the image you had above, you'll need to throw in some spaces, and make sure that every line is the same length. 要制作上面的图像,您需要在空格中加上一些空格,并确保每一行的长度相同。 If it's okay, I think what you're asking is way easier with only one loop. 如果可以的话,我想您要问的是只有一个循环就更容易了。 Try this: 尝试这个:

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

int main() {
int height = 4;
int line = height * 4;
for( int i = height; i > 0; --i ){
        string stars (i, '*');
        int space = line - (i * 2);
        string spaces (space, ' ');
        cout << stars << spaces << stars << endl;
    }
}

This code produces: 此代码产生:

****        ****
***          ***
**            **
*              *

which seems to have a few more spaces than your example up there, but you can fix that by adding a variable before the loop for the maximum length of space you want, and then decrementing it by two each time through the loop. 它似乎比您的示例要多一些空间,但是您可以通过以下方法解决此问题:在循环之前添加一个变量,以获得所需的最大空间长度,然后在循环中每次将其减2。

This uses 3 loops inside another loop 这在另一个循环中使用了3个循环

const int ROW = 4;
const int GAP = 7;

for (int i=ROW, g=GAP; i>=0; i--, g+=2)
{
    for (int j=0; j<i; j++) cout << '*';
    for (int j=0; j<g; j++) cout << ' ';
    for (int j=0; j<i; j++) cout << '*';
    cout << '\n';
}

Output 输出量

****      ****
***        ***
**          **
*            *

Live code 现场代码

You can try this 你可以试试这个

int lf=4,md=4, N=4;
for( int i = 1; i<=N; i++ )
{
   for( int j =1; j<=lf; j++ )
      cout<<"*";
    for( int j =1; j<=md; j++ )
      cout<<" ";
    for( int j =1; j<=lf; j++ )
      cout<<"*";
    cout<<"\n";
       lf--;
      md+=2;
}
void roadBound(int n) {
    const int gap = 6;
    string str = string(n, '*') + string(gap, ' ') + string(n, '*');
    int f=n,b=n+gap-1;
    while(n--){
        str[f--]=str[b++]=' ';
        cout << str << endl;
    }
}

First triangle 第一个三角形

*****
 ****
  ***
   **
    *

This would be the code for the above triangle: 这将是上述三角形的代码:

for (int i = 5; i > 0; i--){
   for (int j = 5; j > 0 ; j--){
      if (i < j){
         cout << ' ' ;;
      }
      else {
         cout << '*';
      }
   }
   cout << endl;
}

Second triangle 第二个三角形

*****
****
***
**
*

This would be the code for the other triangle : 这将是另一个三角形的代码:

for (int i = 5; i > 0; i--){
   for (int j = 0; j < i ; j++){
      cout << '*';  
   }
   cout << endl;
}

There is no need for three loops within another loop. 另一个循环中不需要三个循环。 You can do both of these with only two loops. 您只需两个循环即可完成这两个操作。 One of them just needs a conditional to determine whether to place an asterisk or a space. 其中之一只需要一个条件即可确定要放置星号还是空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM