递归一

题目描述

请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。

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

测试样例:

1
返回:["down"]
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
     
 
        int n = sc.nextInt();
        FoldPaper paper=new FoldPaper();
        String s = paper.fold(1, n, true);
        char[] c = s.toCharArray();
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '0') {
                sb.append("\"" + "up" + "\"").append(",");
            } else {
                sb.append("\"" + "down" + "\"").append(",");
            }
        }
        System.out.println(sb.substring(0, sb.length() - 1) + "]");// sb.length()-1,去掉","
}
        sc.close();
    }
}
    class FoldPaper {
        public StringBuffer sb = new StringBuffer();
 
        public String fold(int index, int n, boolean isDown) {
 
            if (index > n) {
                return null;
            }
            fold(index + 1, n, true);
            if (isDown) {
                sb.append("1");
            } else {
                sb.append("0");
            }
            fold(index + 1, n, false);
            return sb.toString();
 
        }
 
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值