2020软件工程作业04

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

1.寻找数组中第K大是数 考察算法:排序算法
解题思路
我日常相对来说熟悉Java一些就使用Java写。
使用Scanner来获取用户输入。
再利用Array来进行排序
最后再输出结果
解题代码如下:

package hanping;
import java.util.Arrays;
import java.util.Scanner;
public class suanfa {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入序列长度");
		int i = scanner.nextInt();// 获取序列的长度并传入i
		int[] changdu = new int[i];
		System.out.println("请输入数列");
		for (int a = 0; a < i; a++) 
		{
			changdu[a] = scanner.nextInt();
		}
		System.out.println("请输入个数");
		int k = scanner.nextInt();
		int[] geshu = new int[k];
		for (int j = 0; j < k; j++) {
			System.out.println("请输入从第几个数开始和从第几个数结束,第几大的数");
			int l = scanner.nextInt();
			int m = scanner.nextInt();
			int n = scanner.nextInt();
			int[] cup = new int[m - l + 1];
			for (int one = 0; one < m - l + 1; one++) {
				cup[one] = changdu[one + l - 1];
			}
			Arrays.parallelSort(cup);
			geshu[j] = cup[m - l + 1 - n];
		}
		for (int y = 0; y < k; y++) 
		{
			System.out.println("这个数是" + geshu[y]);
			scanner.close();
		}
	}
}

2.二叉树的先、中、后 序遍历与层级遍历 考察算法

代码如下:

package handing;

import java.util.LinkedList;

public class bianli {

    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 root) {
        // TODO 先序遍历
    	 if (root == null) return;
         System.out.print(root.data +" ");
         A(root.l);
         A(root.r);
    }
    private static void B(Node root) {
        // TODO 中序遍历
    	if (root == null) return;
        B(root.l);
        System.out.print(root.data+" ");
        B(root.r);
    }
    private static void C(Node root) {
        // TODO 后续遍历
    	 if (root == null) return;
         C(root.l);
         C(root.r);
         System.out.print(root.data +" ");
    }

    private static void D(Node root) {
        // TODO 层级遍历
    	LinkedList list= new LinkedList<>();
        list.add(root);
        while(! list.isEmpty()) {
            Node ww=list.pop();
            if(ww.l !=null) {
                list.offer(ww.l);
            }
            if(ww.r !=null) {
                list.offer(ww.r);
            }
            System.out.print(ww.data +" ");
        }
    }

    // 构建一颗树,返回根节点
    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 20:08  我怎能不biantai  阅读(76)  评论(0编辑  收藏  举报