当前位置:网站首页>微软面试题:打印折纸的折痕
微软面试题:打印折纸的折痕
2022-06-23 04:31:00 【明朗晨光】
1、题目
请把一段纸条竖着放置在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时折痕是凹下去的,即折痕突起的方向指向纸条的背面。 如果从纸条的下边向上方连续对折2次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。
给定一个输入参数 N N N,代表纸条都从下边向上方连续对折 N N N 次。 请从上到下打印所有折痕的方向。
示例1:
输入:N = 1
输出:down
示例2:
输入:N = 2
输出:down down up
2、分析
通过实践得知:
- 对折一次的折痕:1凹
- 对折两次的折痕:2凹 1凹 2凸
- 对折三次的折痕:3凹 2凹 3凸 1凹 3凹 2凸 3凸
可见,每增加一次折痕,就是在原折痕的左侧新增了一条凹,右侧新增了一条凸。
如果想从上往下打印所有折痕,它就是一棵二叉树的中序遍历序列:
且这棵二叉树有着明确的规则:
- 根节点是 凹 的
- 所有左子树的根是 凹 的
- 所有右子树的根是 凸 的
3、实现
C++ 版
/************************************************************************* > File Name: 033.打印折纸的折痕.cpp > Author: Maureen > Mail: [email protected] > Created Time: 三 6/22 14:20:40 2022 ************************************************************************/
#include <iostream>
using namespace std;
/** * 当前来到了一个节点(折痕),脑海中想象的 * 这个节点在第 i 层,共有 n 层,n 固定不变 * 这个节点如果是凹的,down = true * 这个节点如果是凸的,down = false * 函数功能:中序打印以你想象的节点为头的整棵树 * 额外空间复杂度:O(N),虽然节点数是 2^N - 1,但是实际占用的空间是O(N), * N 是层数也是递归深度,用递归模拟了想象,并没有生成实际的树 */
void process(int i, int n, bool down) {
if (i > n) return ;
//中序打印
process(i + 1, n, true); //左孩子
//cout << i << (down ? "凹" : "凸");
cout << (down ? "凹" : "凸");
process(i + 1, n, false);//右孩子
}
void printAllFolds(int n) {
process(1, n, true);
cout << endl;
}
int main() {
int n;
cin >> n;
printAllFolds(n);
return 0;
}
边栏推荐
- Basic calculator II for leetcode topic analysis
- vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
- Activity启动模式和生命周期实测结果
- ant使用总结(三):批量打包apk
- The official artifact of station B has cracked itself!
- jvm-03. JVM memory model
- 使用链表实现两个多项式相加和相乘
- jvm-04.对象的内存布局
- PAT 乙等 1013 C语言
- Excel sheet column title for leetcode Title Resolution
猜你喜欢

True MySQL interview question (21) - Finance - overdue loan

Memory analysis and memory leak detection

The hierarchyviewer tool cannot find the hierarchyviewer location
![[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project](/img/ce/2e0b0c0f6fae24b5b0df2680229284.png)
[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project

jvm-01. Instruction rearrangement

iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?

Kotlin Android simple activity jump, simple combination of handler and thread

True MySQL interview question (24) -- row column exchange

Digital collections - new investment opportunities
![[cocos2d-x] screenshot sharing function](/img/fc/e3d7e5ba164638e2c48bc4a52a7f13.png)
[cocos2d-x] screenshot sharing function
随机推荐
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
What benefits have digital collections enabled the real industry to release?
Vant web app calendar component performance optimization calendar add min date the minimum date page loads slowly
Pyinstaller 打包pyttsx3 出错
Real MySQL interview questions (25) -- common group comparison scenarios
云原生数据库是未来
金融科技之高效办公(一):自动生成信托计划说明书
How to specify the output path of pig register Project Log
【开源项目】excel导出lua配置表工具
PAT 乙等 1011 C语言
Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL
PAT 乙等 1020.月饼
如何指定pig-register项目日志的输出路径
Summary of ant usage (I): using ant to automatically package apk
Wireshark TS | 视频 APP 无法播放问题
Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
The construction of digital factory can be divided into three aspects
Behind the hot digital collections, a strong technical team is needed to support the northern technical team
HierarchyViewer工具找不到 HierarchyViewer位置
Real MySQL interview questions (XXVI) -- didi 2020 written examination questions