输出纸片折痕方向

请把纸条竖着放在桌上,然后从纸条的下边向上对折,压出折痕后再展 开。此时有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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值