在C 中使用递归绘制图案
Drawing a pattern using recursion in c++
使用C 14,我尝试使用递归来绘制以下模式:我试图绘制的模式。
void drawPattern(int width, int startcol){
if (width > 0){
for (int i=0; i<width; i++){
cout << "*";
}
for (int i=0; i<startcol; i++){
cout << " ";
}
cout << "n";
drawPattern(width/2, startcol);
drawPattern(width/2, startcol+1);
for (int i=0; i<startcol; i++){
cout << " ";
}
for (int i=0; i<width; i++){
cout << "*";
}
}
}
我一直很难将头缠绕在递归上。这个让我难过。
从图像中,我们看到下半场就像上半场一样,除了下半场在上半场之后开始了他的位置。第一和最后一行绘制n-thengent'*'如果n是当前长度。
#include <iostream>
using namespace std;
void Draw(int pos, int len, bool fw) {
cout << string(pos, ' ') << string(len, '*') << endl;
if (len > 1) {
Draw(pos, len / 2, true);
Draw(pos + len / 2, len / 2, false);
}
cout << string(pos, ' ') << string(len, '*') << endl;
}
int main() {
Draw(0, 8, true);
return 0;
}
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 递归无序映射
- TSP递归解的迭代形式
- 如何在Elixir中调用递归函数并行
- 返回递归调用和仅递归调用的区别
- 数组元素打印的递归方法
- 使用递归时获取变量的奇怪值
- 如何在C++中递归地按相反顺序打印集合
- 如何以这种格式使用递归绘制 BST?
- 在C 中使用递归绘制图案
- 尝试递归地重新绘制所有图像像素
- 在扫雷板上递归绘制地雷
- QWidget::重新绘制:更新进度条时检测到递归重新绘制