无法掌握嵌套循环的写作技巧
Unable to get hang of writing nested for loops
我必须在这个图的中间部分使用嵌套的循环:
+------+ <-- line
| ^^ | <-- This is the first line of body
| ^ ^ | <-- This is the 2nd line of body
|^ ^| <-- This is the 3rd line of body
+------+ <-- line
对于行,我写道:
void line()
{
int i, width = 6;
cout << "+";
for(i=0; i<width; i++)
{
cout << "-";
}
cout << "+" << endl;
}
并且它正确输出。但是我找不到嵌套的 for 循环来产生 2 行之间的输出 e
提示:输出 6x6 框的 for 循环如下所示:
for(int i = 1; i <= 6; i++){
for(int j = 1; j <= 6; j++){
cout << "*";
}
cout << endl;
}
因此,下一步是弄清楚使字符在不同的空间中有所不同。下面是向 for 循环添加条件以更改不同空格字符的示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int l = 3;
int r = 4;
for(int i = 1; i <= 6; i++){
for(int j = 1; j <= 6; j++){
if(j == l || j == r)
cout << "^";
else
cout << "*";
}
l -= 1;
r += 1;
cout << endl;
}
}
我希望这有帮助!
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 了解嵌套循环打印星号图案
- 无法掌握嵌套循环的写作技巧
- 在 c++ 中实现嵌套循环的更短方法吗?
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 毕达哥拉斯三重嵌套循环误解
- T(n) 表示嵌套循环
- 2 个嵌套循环的时间复杂度
- 嵌套循环背后的逻辑
- 使用 %s C++嵌套循环
- 嵌套循环和重复迭代器
- 如何在 c++ 下使用嵌套循环和正则表达式降低时间复杂度?
- C++在乘法图中放置随机值(嵌套循环)
- 如何使用 OpenMP 减少嵌套循环?
- 为什么使用 2 个嵌套循环 O(n^2) 复杂度来解决二和问题,当只改变循环计数器逻辑时运行得更快?
- 学习嵌套循环C++与示例混淆
- 如何在CUDA中嵌套循环
- std::vector上的嵌套循环
- 具有动态数组分配的OpenMP嵌套循环
- 两个嵌套循环的运行时间复杂性:二次型还是线性