左神基础班——折纸问题

 

coding为什么写,请看左神视频,或者自己拿纸折一下,

第一次对折,在折痕上写1 下

第二次对折,会在第一个折痕上出现两道折痕, 上面写 2 下,下面的折痕写2上

第三次对折吗, 会在所有第二次的折痕上下都出现两道折痕,一样去写。。。。。发现规律。。。。。二叉树

 

#include<iostream>
using namespace std;
void Fold(int n);
void printFold(int i, int n, bool flag);
int main(){
	Fold(3);
	return 0;
}
void Fold(int n){
	printFold(1,n,true);
}
//模拟递归,神来之笔
void printFold(int i, int n, bool flag){
	if(i <= n){
		//用当前层数和bool标志位来模拟二叉树的中序访问
		//原来是用指针移动访问的,现在用层数➕1表示下一层,且由于左右节点的值是固定的
		//因此直接用true or false表示访问的是左节点还是右节点,妙啊~~
		printFold(i+1, n, true);
		string s = flag? "down":"up";
		cout << s << endl;
		printFold(i+1, n, false);
	}
}

 

相关文章:

  • 2021-12-22
  • 2021-11-06
  • 2021-05-03
  • 2022-01-11
  • 2022-12-23
  • 2021-12-01
  • 2021-06-25
  • 2021-07-31
猜你喜欢
  • 2021-08-23
  • 2021-12-12
  • 2022-12-23
  • 2021-04-19
  • 2022-12-23
相关资源
相似解决方案