2020软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 <排序算法,dfs + bfs搜索算法>
其他参考文献 <算法分析>

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

解题思路

先拿到用户输入的数据个数n
创建 一个 数组来存储 用户输入的数据值 数组长度为 n
对用户输入的数据行进行读取读取使用存储到数组中
用list集合来存储 l, r, m 数据 ,集合长度代表输入的数据行数
调用getMax 方法 获得 第K个最大值

运行代码

package 数组;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class 给定序列中第K大的数 {

public static void main(String[] args) {
	kMax();
}


static Scanner sc = new Scanner(System.in);

public static void kMax() {
	//数组的长度
	int n = sc.nextInt();
	//创建数组
	int[] nums = new int[n];
	//读取 输入 的数据 并存放在数组中 
	sc.nextLine();
	String line = sc.nextLine();
	//对 输入行 进行 读取
	String[] split = line.split(" ");
	for (int i = 0; i < split.length; i++) {
		nums[i] = Integer.parseInt(split[i]);
	}
	
	int k = 0;
	int m = sc.nextInt();
	sc.nextLine();
	//声明一个list来接收每一行的数据
	List<int[]> list = new LinkedList<int[]>();
	while(k < m) {
		String str = sc.nextLine();
		String[] split2 = str.split(" ");
		int[] arr = new int[3];
		for (int i = 0; i < split2.length; i++) {
			arr[i] = Integer.parseInt(split2[i]);
		}
		list.add(arr);
		k++;
	}
	//解析这个list
	for (int[] arr : list) {
		int max = getMax(nums, arr[0], arr[1], arr[2]);
		System.out.println(max);
	}
	
}

/**
 * 
 * @param nums 升序排序的数组
 * @param begin 开始位置
 * @param end	结束位置
 * @param k 第几个最大的值
 * @return
 */
public static int getMax(int[] arr, int begin, int end, int k) {
	int[] nums = new int[end - begin + 1];
	System.arraycopy(arr, begin - 1, nums, 0, nums.length);
	//System.out.println(Arrays.toString(nums));
	Arrays.sort(nums);
	int count = 1;
	for (int i = nums.length - 1; i >= 0; i--) {
		if(count++ == k) {
			return nums[i];
		}
	}
	return 0;
}

}

运行结果

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

解题思路

确定好前中后序以及层级遍历的概念
前序:开头是头结点
中序:根据头结点划分左右子树的元素
后序:末尾是头结点
层级:对二叉树中各个结点进行访问
构建一棵树,按顺序执行

运行代码

package 二叉树;

import java.util.LinkedList;

public class TraversalBiraryTree {

static Node root;

public static void main(String[] args) {
	root = into();
	// 先序遍历
	A();
	// 中序遍历
	B();
	// 后续遍历
	C();
	// 层级遍历
	D();
}

private static void A() {
	// TODO 先序遍历
	System.out.println("先序遍历 : ");
	A(root);
	System.out.println();
}

private static void A(Node node) {
	if(node == null) return;
	System.out.print(node.data + " ");
	A(node.l);
	A(node.r);
}


private static void B() {
	// TODO 中序遍历
	System.out.println("中序遍历 : ");
	B(root);
	System.out.println();
}

private static void B(Node node) {
	if(node == null) return;
	B(node.l);
	System.out.print(node.data + " ");
	B(node.r);
}

private static void C() {
	// TODO 后续遍历
	System.out.println("后续遍历 : ");
	C(root);
	System.out.println();
}

private static void C(Node node) {
	if(node == null) return;
	C(node.l);
	C(node.r);
	System.out.print(node.data + " ");
}

private static void D() {
	// TODO 层级遍历
	System.out.println("层级遍历 : ");
	if (root == null) return;
	LinkedList<Node> queue = new LinkedList<>();
	queue.add(root);
	while (!queue.isEmpty()) {
		Node pop = queue.pop();
		System.out.print(pop.data + " ");
		if (pop.l != null) {
			queue.add(pop.l);
		}
		if (pop.r != null) {
			queue.add(pop.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-29 17:49  oxygen123  阅读(49)  评论(0编辑  收藏  举报