折纸问题java实现

 1 /**
 2      * 折纸问题 这段代码写的太low了 本人水平有限 哎。。。 全是字符串了
 3      * @param n
 4      * @return
 5      * @date 2016-10-7
 6      * @author shaobn
 7      */
 8     public static String[] flodpaper(int n){
 9         int length = (int)Math.pow(2,n)-1;
10         int position = (int)Math.pow(2,n-1)-1;
11         String[] strings = new String[length];
12         strings[position] = "down";
13         if(n==1){
14             return strings;
15         }else {
16             String string_2 = new String();
17             for(String string:flodpaper(n-1)){
18                 string_2+=string;
19                 string_2+=" ";
20             }
21             string_2+="down";
22             string_2+=" ";
23             String string_3 = new String();
24             for(String string:flodpaper(n-1)){
25                 if(string.equals("down")){
26                     string_3+="up";
27                     string_3+=" ";
28                 }else {
29                     string_3+="down";
30                     string_3+=" ";
31                 }
32             }
33             String[] strings4 = string_3.split(" ");
34             for(int i = strings4.length-1;i>=0;i--){
35                 string_2+=strings4[i];
36                 string_2+=" ";
37             }
38             String[] strings2 = string_2.split(" ");
39             return strings2;
40         }
41     }

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

给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up"
原文地址:https://www.cnblogs.com/assassin666/p/5936253.html