软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 学习使用算法
其他参考文献
学号 20189660

题目名称
第一题:寻找数组中第K大是数 考察算法:排序算法
解题思路
第一步:创建scanner类对象,获取能从键盘输入所需要的数据
第二步:定义一个n表示序列长度,数组num,m表示询问的个数,数组s,存输出第k大的数,定义存放查询指令的二维数组arr,其中第一个数l、第二个数r表示数组num中从第l个数到第r个数,第三个数k表示第k大,定义一个数组num1,用来存放所取的数组段
第三步:把num1数组正序排列得到第k大的值,并输出
解题代码

  package com.suanfa;

  import java.util.Scanner;

  public class sf {

        public static void main(String[] args) {
              // TODO Auto-generated method stub
              int l,r,k;
              Scanner scanner = new Scanner(System.in);
              //定义一个数n,表示序列长度
              int n = scanner.nextInt();
              //定义一个数组num,n表示序列
              int[] num = new int[n];
              for (int i = 0; i < n; i++) {
                    num[i] = scanner.nextInt();
              }
              //定义一个数m,表示询问个数
              int m = scanner.nextInt();
              //定义一个数组s,存输出第k大的数
              int [] s= new int[m];
	
              //定义存放查询指令的数组arr
              int[] arr = new int[m]; 
              for (int i = 0; i < m; i++) {  
                    l = scanner.nextInt();  
                    r = scanner.nextInt();  
                    k = scanner.nextInt();  
    
                    //定义一个数组num1,用来存放所取的数组段
                    int[] num1 = new int[r-l+1];
                    for (int a = 0; a < num1.length; a++) {
                          num1[a] = num[a+l-1];
                    }
    
                    //遍历数组num1,寻找到第k大的值
                    for( int b = 0; b < k; b++ ){
                          for( int j = r - l; j > 0; j-- ){
        		            if( num1[ j ] > num1[ j -1 ] ){
        		                  int temp = num1[ j ];
                                          num1[ j ] = num1[ j - 1 ];
        			          num1[ j - 1 ] = temp;
                	            }
        		            s[i]=num1[k -1];
        	              }
                    }
               }
               for (int i = 0; i < m; i++) {
                     System.out.println(s[i]);
               }
        }  
  }

运行得到的结果如下:

题目名称
第二题:二叉树的先、中、后序遍历与层级遍历 考察算法: dfs + bfs搜索算法
解题思路
先序遍历:根节点→左子树→右子树
中序遍历:左子树→根节点→右子树
后序遍历:左子树→右子树→根节点
层次遍历:按层次遍历,左子树右子树依次进入序列
解题代码

package com.suanfa;

import java.util.LinkedList;

public class erchashu {

        public static void main(String[] args) {
              // TODO Auto-generated method stub

              /*
              作业要求:叉树的先、中、后 序遍历与层级遍历
              自己实现四个方法,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 先序遍历
              if (root!=null){
                    System.out.print(root.data + " ");
		A(root.l);
		A(root.r);
              }
        }
        private static void B(Node root) {
              // TODO 中序遍历
              if (root!=null){
                    B(root.l);
                    System.out.print(root.data + " ");
                    B(root.r);
              }
        }
        private static void C(Node root) {
              // TODO 后续遍历
              if (root!=null){
                    C(root.l);
                    C(root.r);
                    System.out.print(root.data + " ");
              }
        }

        private static void D(Node root) {
              // TODO 层级遍历
              if (root != null) {
                    //创建一个LinkedList 链表
                    LinkedList<Node> queue = new LinkedList<Node>();
                    queue.add(root);
                    Node node = null;
                    while (!queue.isEmpty()) {
                          //pop()栈方法等同于removeFirst(),表示从堆栈中弹出栈顶元素
                          node = (Node) queue.pop();
                          System.out.print(node.data + " ");
                          if (node.l != null) {
                                queue.add(node.l);
                          }
                          if (node.r != null) {
                                queue.add(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;
              }
        }

  }

运行得到的结果如下:

posted @ 2020-10-27 19:35  喵aab  阅读(82)  评论(0编辑  收藏  举报