2020软件工程作业04
第一题:寻找数组中第K大的数
解题思路
1.首先要定义一个数组,第一次输入的数字作为数组的长度;
2.再输入一组数字作为数组元素数组中,获得数组的值;
3.然后输入数字作为新的数组的长度;
4.再输入要询问的个数,然后进行for循环;
5.最后采用冒泡排序法求出k
解题代码
import java.util.Arrays;
import java.util.Scanner;
public class xxy {
public static void main(String[] args) {
//输入准备
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int arr[] = new int[n+1];
for(int i=1;i<=n;i++) {
arr[i] = in.nextInt();
}
int m = in.nextInt();
int []l = new int[m];
int []r = new int[m];
int []k = new int[m];
for(int i=0;i<m;i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
k[i] = in.nextInt();
}
int ans[] = new int[m];
int index = 0;
//一次循环表示一次寻找,找到了放进ans数组中,
for(int i=0;i<m;i++) {
//计步器
int cnt = 0;
//标记数组,一次循环可以找出最大值,那么在找次大的时候,不能再与这个最大的比较
//用1表示已经访问过了,初始0表示还没访问过
int flag[] = new int[n+1];
for(int j=l[i];j<=r[i];j++) {
cnt++;//计数
int max = 0;
int maxIndex = 0;
//一次循环找最大
for(int t=l[i];t<=r[i];t++) {
if(arr[t]>max&&flag[t]!=1) {
max = arr[t];
maxIndex = t;
}
}
//标记
flag[maxIndex] = 1;
//判断是否达到k,达到便存起答案,退出循环
if(cnt==k[i]) {
ans[index++] = max;
break;
}
}
}
for(int i=0;i<index;i++) {
System.out.println(ans[i]);
}
}
运行效果
第二题:二叉树的先、中、后 序遍历与层级遍历
解题代码
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("\n"+"前序遍历:");
A(root);
// 中序遍历
System.out.print("\n"+"中序遍历:");
B(root);
// 后续遍历
System.out.print("\n"+"后序遍历:");
C(root);
// 层级遍历
System.out.print("\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 中序遍历
System.out.print(node.data+"\t");
if(node.l !=null){
B(node.l);
}
if(node.r !=null){
B(node.r);
}
}
private static void C(Node node) {
// TODO 后续遍历
System.out.print(node.data+"\t");
if(node.l !=null){
C(node.l);
}
if(node.r !=null){
C(node.r);
}
}
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;
}
}
运行效果