软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 <算法与数据结构>
学号 <20189750>

第一题:寻找数组中第K大是数

定义一个数组接收从键盘输入的数据序列,再通过排序算法将数组中的元素数据从小到大排序。
再根据从键盘随后输入的数字来寻找在序列中相应第K大的数据。

使用语言: JAVA

代码:

package lijian_20189750;

import java.util.Scanner;

/***********************************************************
 *  作者          时间                  项目名
 *  李健     2020/10/26 17:47        MyTestProject
 *  功能:
 ************************************************************/
public class test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入序列长度:");
        int nums = sc.nextInt();
        //定义一个存放序列的数组
        int[] numArray = new int[nums];
        //存放数据
        System.out.println("请输入要查询的序列数据:");
        for (int i = 0; i < nums; i++){
            numArray[i] = sc.nextInt();
        }
        /*将得到的数据进行排序
        * 这里我将采用插入算法
         */
        int n = numArray.length; //这里用一个变量来储存数组的长度变量,防止排序的时候多次调用length方法影响性能
        int temp,j; //定义一个用来储存临时数据的变量,用来中转数据
        for (int i = 1; i < n;i++){ //排序次数
            temp = numArray[i];
            ////取i前面的所有跟i位置元素进行比较,先比较i-1和i,如果i-1大于i,则互换位置,i-1和i-2比较,以此类推
            for (j = i-1;j>=0&&numArray[j]>temp;j--){
                numArray[j+1] = numArray[j];
            }
            numArray[j+1] = temp;
        }
        //排序成功以后 我们接收询问个数的变量
        System.out.println("请输入要查询的个数");
        int askNum = sc.nextInt();
        int[] result = new int[askNum];
        //定义两个用来储存我们所需要的变量 新数组的起始
        int one = 0;
        int sec = 0;
        for (int i = 0; i < askNum;i++){
            System.out.println("第"+(i+1)+"次:");
            int first = sc.nextInt();
            int second = sc.nextInt();
            int last = sc.nextInt();
            //得到其实数组的两个数据下标
            for (int h = 0; h < n;h++){
                if(numArray[h]==first){
                    one = h;
                }
                if(numArray[h]==second){
                    sec = h;
                }
            }
            int newNum = sec - one + 1;
            int[] newArray = new int[newNum];
            //克隆我们要的数组
            System.arraycopy(numArray, one, newArray, 0, newNum);
            result[i] = newArray[newNum-last];
        }
        System.out.println("最终结果如下:");
        for (int k = 0; k < result.length;k++){
            System.out.println(result[k]);
        }
    }
}

运行截图

第二题:二叉树的先、中、后 序遍历与层级遍历

使用语言: JAVA

代码:

import java.util.LinkedList;
public class BinaryTreeDemo {
    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-26 19:04  李0v0健  阅读(149)  评论(0)    收藏  举报