首页 > 试题广场 >

折纸问题

[编程题]折纸问题
  • 热度指数:3290 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请把一张纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时折痕是凹下去的,即折痕突起的方向指向纸条的背面。如果从纸条的下边向上方连续对折2次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。给定一个输入参数N,代表纸条都从下边向上方连续对折N次,请从上到下打印所有折痕的方向。
[要求]
时间复杂度为,额外空间复杂度为

输入描述:
第一行一个整数N。表示对折次数


输出描述:
输出若干行,若该折痕向下,输出"down",否则输出"up"
示例1

输入

1

输出

down
示例2

输入

2

输出

down
down
up

备注:
import java.util.*;
public class Main{
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        printAllFlods(Integer.valueOf(in.nextLine().trim()));
    }
    
    public static void printAllFlods(int fTimes) {
        if(fTimes<1)
            return;
        printProcess(1,fTimes,true);
    }
    public static void printProcess(int index,int n,boolean isDown) {
        if(index==n) {
            System.out.println(isDown?"down":"up");
            return;
        }
        printProcess(index+1,n,true);
        System.out.println(isDown?"down":"up");
        printProcess(index+1,n,false);
    }
}
发表于 2021-11-05 14:04:21 回复(0)

问题信息

上传者:小小
难度:
1条回答 4660浏览

热门推荐

通过挑战的用户

查看代码