2020软件工程作业04
| 这个作业属于软件工程 | https://edu.cnblogs.com/campus/zswxy/2018SE/ |
|---|---|
| 这个作业要求 | https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406 |
| 这个作业的目标 | 算法与数据结构 |
第一题
-
题目名称
寻找数组中第K大数 考察算法:排序算法 -
解题思路
- 1.定义Scanner类用来获取用户输入、定义数组并初始化
- 2.手动实现一个排序算法,我手动实现了一个java语言的快速排序
- 3.输出第k大的数
-
解题代码
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Miate
*/
public class paixun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] number = new int[num];
for (int i = 0; i < num; i++) {
number[i] = sc.nextInt();
}
// System.out.println(Arrays.toString(number));
int ask = sc.nextInt();
int[] a = new int[ask];
for (int i = 0; i < ask; i++) {
int l = 0;
l = sc.nextInt();
int r = 0;
r = sc.nextInt();
int k = 0;
k = sc.nextInt();
int[] b = new int[r - l + 1];
for (int j = 0; j < b.length; j++) {
b[j] = number[j + l - 1];
}
qck(b); //调用算法
a[i] = b[r - l + 1 - k];
}
System.out.println("--------------------");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
//快速排序算法
//声明静态的qck方法,在该方法中判断传入的数组是否为空,如果不为空,则调用unSt方法进行排序
public static void qck(int[] str) {
if (str.length > 0) {
//查看数组是否为空
unSt(str, 0, str.length - 1);
}
}
//创建静态的unSt方法,在该方法中判断l参数是否小于r参数,如果是则调用getMd方法,将数组一分为二,并且调用自身的方法进行递归排序
public static void unSt(int[] list, int l, int r) {
if (l < r) {
int md = getMd(list, l, r); //将数组分成两份
unSt(list, l, md - 1); //小的排序
unSt(list, md + 1, r); //大的排序
}
}
//声明静态方法getMd,返回一个int
public static int getMd(int[] list, int l, int r) {
int num = list[l]; //第一个值作为分界线
while (l < r) {
while (l < r && list[r] > num) {
r--;
}
list[l] = list[r]; //数小的到第一个
while (l < r && list[l] < num) {
l++;
}
list[r] = list[l]; //大的最后一个
}
list[l] = num;
return l;
}
}
- 运行结果:

第二题
-
题目名称
二叉树的先、中、后 序遍历与层级遍历 考察算法: dfs + bfs搜索算法 -
解题思路
使用java实现- 1.先序遍历 根结点 ---> 左子树 ---> 右子树
- 2.中序遍历 左子树 ---> 根结点 ---> 右子树
- 3.后续遍历 左子树 ---> 右子树 ---> 根结点
- 4.层级遍历 遍历根结点,读出数据--->左子树右子树依次进队列
-
解题代码
import com.sun.xml.internal.fastinfoset.Decoder;
import java.util.Collections;
import java.util.LinkedList;
/**
* @author Miate
*/
public class er {
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;
}
}
}
- 运行结果:


浙公网安备 33010602011771号