软件工程作业04

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

1.题目名称:用排序算法找数组中第K大的数
解题思路:A[1001]存储n个数据,Temp[1001]存储A[ ]中第 l 个到第 r 个的数据,然后对Temp进行sort排序,
最后找出从小到大的第(length+1-k)的数据(就是从大到小的第k个数据)
解题代码
package puyuexing;
import java.util.Arrays;
import java.util.Scanner;

public class pu {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	// 定义序列长度
	int n = sc.nextInt();
	// 定义序列
	int[] a = new int[n];
	// 依次输入序列
	for (int i = 0; i < n; i++) {
		a[i] = sc.nextInt();
	}
	// 定义询问s个数
	int s = sc.nextInt();
	// 每行有三个数
	int[][] arr = new int[s][3];
	// 输入的三个数
	for (int i = 0; i < s; i++) {
		// 第s行第1个数
		arr[i][0] = sc.nextInt();
		arr[i][1] = sc.nextInt();
		arr[i][2] = sc.nextInt();
	}
	
	sc.close();//关闭输入资源
	
	for (int i = 0; i < s; i++) {
		System.out.println(getMaxk(a, arr[i][0], arr[i][1], arr[i][2]));
	}
}

public static int getMaxk(int[] a, int begin, int end, int k) {
	int aa[] = new int[end - begin + 1];// 创建一个数组,长度为end
	// 用来装表示的开始到结束的数组,第几个到第几个,谁最大
	for (int i = 0; i < aa.length; i++) {
		aa[i] = a[begin + i - 1];
	}
	Arrays.sort(aa);// 进行排序,从小到大
	return aa[aa.length - k];// 第几大
}

}
执行结果

2.题目名称:二叉树的先、中、后 序遍历与层级遍历
解题思路:对于树的遍历:
有递归实现
非递归实现(借助于栈,后进先出)
参差遍历 (借助队列)
解题代码
package puyuexing;

import java.util.LinkedList;

public class zhou {

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=(Node) 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-29 00:00  Bxhdhd  阅读(88)  评论(0编辑  收藏  举报