首页 > 试题广场 >

折纸问题

[编程题]折纸问题
  • 热度指数:9511 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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

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

测试样例:
1
返回:["down"]
想请教一下各位,实在不知道自己这个错在哪里了,在牛客刷题比较少,实际输出不能看到全部?根据正确的代码作为对数器和这个比较,也暂时没发现什么问题,还请各位赐教
import java.util.*;

public class FoldPaper {
    public String[] foldPaper(int n) {
        if (n == 0) return new String[0];
        // write code here
        //就是个中序遍历

        Queue<Node> queue = new LinkedList();
        Node root = new Node("down");
        queue.add(root);
        int height = 1;
                //这里是在用层序遍历建二叉树
        while (true) {
            int num = queue.size();

            while (num > 0) {
                Node cur = queue.poll();
                cur.left = new Node("down");
                queue.add(cur.left);

                cur.right = new Node("up");
                queue.add(cur.right);

                num--;
            }
                        //高度等于n说明建树完毕
            if (++height == n) {
                break;
            }
        }
                //用逗号分隔得到数组
        return inOrder(root).split(",");

    }

    static StringBuilder path = new StringBuilder();
        中序遍历拼接结果,用逗号做分割
    public String inOrder(Node root) {
        if (root == null) return "";
        inOrder(root.left);
        path.append(root.val+",");
        inOrder(root.right);
        return path.toString();
    }
}

class Node {
    String val;
    Node right;
    Node left;
    public Node (String val) {
        this.val = val;
    }
}


编辑于 2022-06-06 18:59:11 回复(0)
import java.util.*;

public class FoldPaper {
    
    public String[] foldPaper(int n) {
        List<String> li = new ArrayList<String>();
        parperDeal(n,1,true,li);
        //String[]arr = (String[])li.toArray();
        String [] str = new String[li.size()];
        for(int i=0;i<li.size();i++){
            str[i] = li.get(i);
        }
        return str;
    }
    
    public void parperDeal(int n,int index, boolean isNotDown,List<String> li ){
        
        if(index == n){
            li.add(isNotDown? "down":"up");
            return;
        }
        parperDeal(n,index+1,true,li);
        li.add(isNotDown?"down":"up");
        parperDeal(n,index+1,false,li);
    }
}
发表于 2021-11-11 14:30:52 回复(0)
import java.util.Scanner;

public class Main{





    //创建树
    public static Node<String> createTree(Node<String> root,int n){        //创建一棵以root为根结点,深度为n的树,返回树的根结点
        //如果n=2,则构建左右子树并退出递归
        if (n==2) {
            //创建树的根结点
            //Node<String> root = new Node("down", null, null);
            root.left=new Node("down", null, null);
            root.right=new Node("up", null, null);
            return root;
        }else if(n>2){
            //创建树的根结点
            //Node<String> root = new Node("down", null, null);
            //创建树的左子树,并让根结点指向左子树
            root.left=new Node("down", null, null);
            root.right=new Node("up", null, null);
            root.left = createTree(root.left,n - 1);
            //创建树的右子树,并让根结点指向右子树
            root.right = createTree(root.right,n - 1);
            return root;
        }
        return root;
    }

    //打印输出
    public  static void printTree(Node<String> x) {
        if (x == null) {
            return;
        }
        if (x.left!=null){
            printTree(x.left);
        }
        System.out.println(x.item);
        if (x.right!=null){
            printTree(x.right);
        }
    }

    //主方法
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        Node<String> root=new Node<>("down",null, null);//根结点
        Node<String> tree=createTree(root,N);
        printTree(root);

    }

    private static class Node<T> {
        //存储结点值
        private T item;
        //存储左孩子结点
        private Node left;
        //存储右孩子结点
        private Node right;
        //构造方法

        public Node(T item, Node left, Node right) {
            this.item = item;
            this.left = left;
            this.right = right;
        }
    }
}

发表于 2020-12-20 14:08:06 回复(0)
import java.util.*;

public class FoldPaper {
    public String[] foldPaper(int n) {
         String mesage = Fribonacci(n);
        String[] array = new String[mesage.length()];
        for (int i = 1; i <= mesage.length() ; i++) {
            array[i - 1] = (String) mesage.subSequence(i - 1, i);
            if (array[i - 1].equals("1")){
                array[i-1]="down";
            }else {
                array[i-1]="up";
            }
        }
       return array;
    }       
 public String Fribonacci(int n) {
        String up = "0", down = "1", other = "2";
        if (n == 1) {
            return down;
        } else {
            String replace1 = Fribonacci(n - 1).replace(down, other);
            String replace2 = replace1.replace(up, down);
            String replace3 = replace2.replace(other, up);
            replace1 = new StringBuilder(replace3).reverse().toString();
            return Fribonacci(n - 1) + down + replace1;
        }
    }
}

发表于 2019-04-20 20:38:04 回复(0)
递归中序遍历
import java.util.*;

public class FoldPaper {
    public String[] foldPaper(int n) {
        
        List<String> list = new LinkedList<String>();
        listProcess(1,n,true,list);
        int len = (2<< n-1) -1;
        String[] strs = new String[len];
        for(int i=0; i<len; i++){
            strs[i] = list.get(i);
        }
        
        return strs;
    }
    
    public void listProcess(int i, int n, boolean isDown, List<String> list){
        if(i>n){
            return;
        }
        listProcess(i+1,n,true,list);
        list.add(isDown?"down":"up");
        listProcess(i+1,n,false,list);
    }
}
发表于 2017-08-02 09:04:42 回复(0)
import java.util.*;

public class FoldPaper {
    
    public String[] foldPaper(int n) {
        // write code here
       if (n == 1){
           String []r=new String[1];
           r[0] = "down";
           return r;
       }else{
           String[] temp = foldPaper(n-1);
           int l= temp.length;
           String r[]=new String[2*l+1];
           for(int i=0;i<l;i++){
               r[i]=temp[i];
               if(temp[i].equals("down")){
                   r[2*l-i]="up";
               }else {
                   r[2*l-i]="down";
               }
           }
           r[l]="down";
           return r;
           
       }
    }
}
发表于 2017-07-27 08:00:41 回复(0)

问题信息

难度:
6条回答 26499浏览

热门推荐

通过挑战的用户

查看代码