2020软件工程作业04
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/2018SE/ |
| ---- | ---- | ---- |
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406 |
|这个作业的目标 | 算法与数据结构 |
|其他参考文献|无|
题目名称:
题1:寻找数组中第K大是数 考察算法:排序算法解题思路:
1、定义五个变量,和三个数组,以方便后面使用2、定义Scanner类,来获取输入的值
3、使用for循环,来获取每次输入的数组数和给数组进行冒泡排序
4、输出
代码:
package oo;
import java.util.Scanner;
public class arith {
public static void main(String[] args) {
int l,r,k;
int a=0;
int n=0;
int m=0;
// int p=0;
Scanner sc=new Scanner(System.in);
// System.out.println("请输入一个数n,表示序列的长度:");
n=sc.nextInt();
// System.out.println("请输入一个数n个正整数,表示序列:");
int [] shuzu1=new int [n];
for(int i=0;i<n;i++) {
shuzu1[i]=sc.nextInt();
}
// System.out.println("请输入一个正整数m,表示询问个数:");
m=sc.nextInt();
int []shuzu2=new int[m];
// System.out.println("请输入三个数:");
for(int i=0;i<m;i++) {
l=sc.nextInt();
r=sc.nextInt();
k=sc.nextInt();
int [] shuzu3=new int[r-l+1];
for(int w=0;w<shuzu3.length;w++) {
shuzu3[w] = shuzu1[w + l - 1];
}
for(int j=0;j<shuzu3.length;j++) {
for(int b=j;b<shuzu3.length;b++) {
if(shuzu3[j]>shuzu3[b]) {
a=shuzu3[j];
shuzu3[j]=shuzu3[b];
shuzu3[b]=a;
}
}
}
// p=shuzu3[r-l+1-k];
// System.out.println(p);
shuzu2[i]=shuzu3[r-l+1-k];
}
for(int i=0;i<m;i++) {
// System.out.println(p);
System.out.println(shuzu2[i]);
}
}
}
运行结果:
题目名称:
题2:二叉树的先、中、后 序遍历与层级遍历解题思路:
先序遍历:根在前,从左往右,一棵树的根永远在左子树前面,左子树又永远在右子树前面中序遍历:根在中,从左往右,一棵树的左子树永远在根前面,根永远在右子树前面
后序遍历:根在后,从左往右,一棵树的左子树永远在右子树前面,右子树永远在根前面
层级遍历:就是按层,从上到下,从左到右遍历
代码:
package oo;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
/*
作业要求:叉树的先、中、后 序遍历与层级遍历
自己实现四个方法,main方法中调用,将结果打印到控制台
*/
/* 二叉树的结构
A
/ \
T 6
/
D
/ \
N 5
/ \ /
B 4 1
\
9
*/
Node root = into();
// 先序遍历
System.out.print("先序遍历:");
A(root);
System.out.println();
// 中序遍历
System.out.print("中序遍历:");
B(root);
System.out.println();
// 后续遍历
System.out.print("后序遍历:");
C(root);
System.out.println();
// 层级遍历
System.out.print("层次遍历:");
D(root);
}
private static void A(Node root) {
// TODO 先序遍历
System.out.print(root.data+" ");
if(root.l!=null) {
A(root.l);
}
if(root.r!=null) {
A(root.r);
}
}
private static void B(Node root) {
// TODO 中序遍历
if(root.l!=null) {
B(root.l);
}
System.out.print(root.data+" ");
if(root.r!=null) {
B(root.r);
}
}
private static void C(Node root) {
// TODO 后序遍历
if(root.l!=null) {
C(root.l);
}
if(root.r!=null) {
C(root.r);
}
System.out.print(root.data+" ");
}
private static void D(Node tree) {
// TODO 层级遍历
if (tree != null) {
LinkedList<Node> linkedList = new LinkedList<Node>();
//先将根节点入队
linkedList.offer(tree);
Node node = null;
while (!linkedList.isEmpty()) {
node = (Node) linkedList.pop();
System.out.print(node.data + " ");
if (node.l != null) {
//将出队结点的左子树根入队
linkedList.offer(node.l);
}
if (node.r != null) {
//将出队结点的右子树根入队
linkedList.offer(node.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;
}
}
}