zoukankan      html  css  js  c++  java
  • 纸条从下往上对折N次,打印每次折痕的方向


    /**
    * 纸条从下往上对折N次,打印每次折痕的方向
    * <p>
    * 通过实践得知:纸条对折N次得到的就是一个N层的满二叉树,且头节点是凹折痕,往下的任一子树的左节点都是凹折痕,右节点都是凸折痕
    */
    public class PaperFolding {

    /**
    * 打印折痕方向
    *
    * @param n 折叠次数
    */
    public static void printPaperFolding(int n) {
    printAllFolds(1, n, true);
    }

    /**
    * 递归打印折痕方向
    *
    * @param i 第几层
    * @param n 折叠总次数
    * @param down 是否向下(凹折痕)
    */
    private static void printAllFolds(int i, int n, boolean down) {
    if (i > n) {
    return;
    }
    printAllFolds(i + 1, n, true);
    System.out.println(down ? "凹" : "凸");
    printAllFolds(i + 1, n, false);
    }

    public static void main(String[] args) {
    printPaperFolding(3);
    }

    /**
    * 二叉树结构
    */
    public static class Node {

    public int value;

    public Node left;

    public Node right;

    public Node(int value) {
    this.value = value;
    }

    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    bzoj1066: [SCOI2007]蜥蜴
    bzoj3504: [Cqoi2014]危桥
    bzoj2756: [SCOI2012]奇怪的游戏
    bzoj1570: [JSOI2008]Blue Mary的旅行
    Ultra-QuickSort
    Bin Packing
    Watering Grass
    区间覆盖
    抄书 Copying Books UVa 714
    分馅饼 Pie
  • 原文地址:https://www.cnblogs.com/laydown/p/12952749.html
Copyright © 2011-2022 走看看