数据结构与算法(JAVA版)7_7:微软纸条折痕问题

题目:请把一段纸条竖着放在桌子上,然后从纸条的下边向上对折1次,压出折痕后展开。此时折痕是凹下去的,即折痕突起的方向指向纸条的背面。如果从纸条的下边向上边连续对折2次,压出折痕后展开,此时有三条折痕,从上到下依次为凹凹凸。
给定一个参数N,代表纸条都从下方向上方连续对折N次。请从上到下打印所有折痕的方向。
数据结构与算法(JAVA版)7_7:微软纸条折痕问题_第1张图片

package com.inspire.chapter7;

public class Code08_PaperFolding {

	public static void printAllFolds(int N) {
		printProcess(1, N, true);
	}

	// 递归过程,来到了某一个节点,
	// i是结点的层数,N一共的层数,down == true 凹 down == false 凸
	public static void printProcess(int i, int N, boolean down) {
		if (i > N) {
			return;
		}
		printProcess(i + 1, N, true);
		System.out.println(down ? "凹 " : "凸 ");
		printProcess(i + 1, N, false);
	}

	public static void main(String[] args) {
		int N = 3;
		printAllFolds(N);
	}
}

你可能感兴趣的:(算法与数据结构)