您的位置:首页 > 其它

输出纸片折痕方向

2016-04-26 23:00 274 查看
请把纸条竖着放在桌上,然后从纸条的下边向上对折,压出折痕后再展 开。此时有1条折痕,突起的方向指向纸条背面,这条折痕叫做“下”折痕;突起的方向向指向纸条正面的折痕叫做“上”折痕。如果每次都从下边向上边对折,对折N次。请从上到下计算出所有折痕的方向。

给定折的次数 n ,请返回从上到下的折痕的数组,若为下折痕则对应元素为”down”,若为上折痕则为”up”.

思路:动手折一下就可以发现规律:n=1时,一条折痕 a,朝下;n=2时,新增两条折痕 b,c,分别在 a 的左右呈现下、上的分布;n=3时,在上次新增两条折痕 b 的左右新增 d,e,呈现下、上分布,c的左右新增 f,g, 呈现下、上分布。这是一个递归的过程,中间为下,左右分别为下上。

#include<iostream>
#include<vector>
#include<string>
const int n = 4;
using namespace std;
void printTree(vector<string> &V, int n, string str){
if (n>0){
printTree(V, n - 1, "down");
V.push_back(str);
printTree(V, n - 1, "up");
}
}

void main(){
vector<string> result;
printTree(result, n, "down");
for (vector<string>::iterator ite = result.begin(); ite != result.end();ite++){
cout << *ite<<" ";
}
cin.get();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: