使用星号的金字塔形状

问题描述 投票:0回答:1

使用星号的金字塔形状

嗨,我是 C++ 新手,我尝试了嵌套循环,但我不知道下一步是什么,看到了一些解决方案,但我不明白。 如果你知道,请写下来。 感谢您的帮助。

c++ c++17
1个回答
0
投票

很简单,但你一定能理解。 例如,我将为您提供一个使用嵌套循环的简单代码。

#include <iostream>
using namespace std;
int main()
{
 for (int i = 1; i < 5; i++) {
    for (int j = 1; j < 3; j++) {
        cout << i + j << endl;
    }
 }
}

据说每执行一次外循环,内循环就会执行3次(j<3). The first time the i of the outer loop will be equal to 1 and then the inner loop will run 3 times and every j iteration will print how much the i+j is equal to and also increase the j. who finishes doing the inner loop 3 times will go back to do it again, do the outer loop again and go into the inner one again... Like this until i is equal to 5

© www.soinside.com 2019 - 2024. All rights reserved.