2020软件工程作业04

这个作业属于哪个课程https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 算法
其他参考文献 博客园

第一题

1、寻找数组中第K大是数 考察算法:排序算法

2、思路

  给定一个序列,每次询问序列中第l到第r个数中第k大的数是哪个。

需要解决的几个问题块:
(1)、输入接收

目标是用java实现该算法,第一行获取n后根据n初始化数组。第二行就根据第一行的长度获取相应的数字,第三个m用二位数组实现.......
(2)、遍历排序接收的数字
(3)、根据接受的第三行包括以后的数据进行逻辑运算并将其结果输出。

(4)、数据约束n,m<=100(太大了怕我电脑带不起),k<=(r-l+1)

3、代码

package homework;
import java.util.Arrays;
import java.util.Scanner;

public class FindMaxNum {

        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            int n=input.nextInt();
            int[] a=new int[n];
            for (int i = 0; i < a.length; i++) {
                a[i]=input.nextInt();
            }
            int m=input.nextInt();
            int[][]tag=new int[m][3];
            for (int i = 0; i < m; i++) {
                tag[i][0]=input.nextInt();
                tag[i][1]=input.nextInt();
                tag[i][2]=input.nextInt();
            }
            input.close();
            for (int i = 0; i < m; i++) {
                System.out.println(getNum(a, tag[i][0], tag[i][1], tag[i][2]));
            }
           
        }
        public static int getNum(int[] a,int b,int e,int k){
            int[] arr=new int[e-b+1];//k<=(r-l+1)
            for (int i =0 ; i < arr.length; i++) {
                arr[i]=a[b-1+i];
            }
            Arrays.sort(arr);
            return arr[arr.length-k];
        }
     

}

截图:

 

 

 第二题

1、二叉树的先、中、后 序遍历与层级遍历 考察算法: dfs + bfs搜索算法

2、思路

先序遍历:对于当前节点,先输出该节点,然后输出它的左孩子,最后输出它的右孩子。
中序遍历:首先遍历左子树,然后访问根结点,最后遍历右子树。若二叉树为空则结束返回。
后序遍历:对于当前结点,先输出它的左孩子,然后输出它的右孩子,最后输出该结点

层级:按层次遍历

 

3、代码

package homework;
import java.util.LinkedList;
public class Tree{


        public static void main(String[] args) {
            /*
                作业要求:叉树的先、中、后 序遍历与层级遍历
                自己实现四个方法,main方法中调用,将结果打印到控制台
             */
            /*  二叉树的结构
                         A
                        / \
                       T   6
                      /
                     D
                   /   \
                  N     5
                 / \    /
                B   4  1
                     \
                      9
             */
            Node root = into();
            // 先序遍历
            System.out.println("\n"+"前序遍历");
            A(root);
            // 中序遍历
            System.out.println("\n"+"中序遍历");
            B(root);
            // 后续遍历
            System.out.println("\n"+"后续遍历");
            C(root);
            // 层级遍历
            System.out.println("\n"+"层级遍历");
            D(root);

        }

        private static void A(Node node) {
            // TODO 先序遍历
            System.out.print(node.data + "\t");
            if(node.l != null){
                A(node.l);
            }
            if(node.r != null){
                A(node.r);
            }

        }
        private static void B(Node node) {
            // TODO 中序遍历
            if(node.l != null){
                B(node.l);
            }
            System.out.print(node.data + "\t");
            if(node.r != null){
                B(node.r);
            }
        }
        private static void C(Node node) {
            // TODO 后续遍历
            if(node.l != null){
                C(node.l);
            }
            if(node.r != null){
                C(node.r);
            }
            System.out.print(node.data + "\t");
        }

        private static void D(Node node) {
            // TODO 层级遍历
            if(node == null) {
                return ;
            }
            LinkedList<Node> queue = new LinkedList<>();
            Node current = null;
            queue.offer(node);
            while(!queue.isEmpty()) {
                current = queue.poll();
                System.out.print(current.data+ "\t");
                if(current.l != null) {
                    queue.offer(current.l);
                }
                if(current.r != null) {
                    queue.offer(current.r);
                }
            }
        }

        // 构建一颗树,返回根节点
        private static Node into(){
            Node root = new Node("A");
            Node node1 = new Node("T");
            Node node2 = new Node("D");
            Node node3 = new Node("N");
            Node node4 = new Node("B");
            Node node5 = new Node("6");
            Node node6 = new Node("5");
            Node node7 = new Node("4");
            Node node8 = new Node("9");
            Node node9 = new Node("1");
            root.l = node1;
            node1.l = node2;
            node2.l = node3;
            node2.r = node6;
            node3.r = node7;
            node7.r = node8;
            node6.l = node9;
            node3.l = node4;
            root.r = node5;
            return root;
        }

        // 节点
        static class Node{
            // 数据
            Object data;
            // 左孩子
            Node l;
            // 右孩子
            Node r;

            public Node(){}

            public Node(Object data) {
                this.data = data;
                this.l = null;
                this.r = null;
            }

            public Node(Object data, Node l, Node r) {
                this.data = data;
                this.l = l;
                this.r = r;
            }
        }
    

}

 

 

 

 

posted @ 2020-10-28 18:59  软件工程4班李浩  阅读(124)  评论(0编辑  收藏  举报