zoukankan      html  css  js  c++  java
  • 2020软件工程作业04

    这个作业属于哪个课程 2018级软件工程 - 中南林业科技大学涉外学院
    这个作业要求在哪里 2020软件工程作业04
    这个作业的目标 测试编程能力
    其他参考文献

    第一题:寻找数组中第K大是数 考察算法:排序算法 总分:50

    解题思路

    复制指定的数组区间,对复制数组进行从小到大的排序,然后得到第k大的数

    代码

    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main{
    
    	public static void main(String[] args) {		
    		int n,m,l,r,k;
    		Scanner input= new Scanner(System.in);
    		
    		n = input.nextInt();
    		int[] array = new int[n];
    		for(int i = 0;i<n;i++) {
    			array[i] = input.nextInt();
    		}
    		
    		m = input.nextInt();
    		for(int i = 0;i < m;i++) {
    			l = input.nextInt();
    			r = input.nextInt();
    			k = input.nextInt();
    			
    			int[] copy= Arrays.copyOfRange(array, l-1, r);
    			Arrays.sort(copy);			
    			System.out.println(copy[copy.length-k]);
    		}
    	}
    
    }
    
    

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

    解题思路

    前序遍历:先根后左右
    中序遍历:先左后根再右
    后序遍历:先左右后根
    层次遍历:遍历根节点,读出数据,然后左节点右节点进队列

    代码

    
    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 tree) {
    		// TODO 先序遍历
    		if (tree != null) {
    			System.out.print(tree.data + " ");
    			A(tree.l);
    			A(tree.r);
    		} 
    		
    	}
    
    	private static void B(Node tree) {
    		// TODO 中序遍历
    		if (tree != null) {
    			B(tree.l);
    			System.out.print(tree.data + " ");
    			B(tree.r);
    		}
    		
    	}
    
    	private static void C(Node tree) {
    		// TODO 后续遍历
    		if (tree != null) {
    			C(tree.l);
    			C(tree.r);
    			System.out.print(tree.data + " ");
    		}
    		
    	}
    
    	private static void D(Node tree) {
    		if (tree != null) {
    			
    			LinkedList<Node> q = new LinkedList<Node>();
    			q.add(tree);
    			Node n = null;
    			while (!q.isEmpty()) {
    				n = (Node) q.pop();
    				System.out.print(n.data + " ");
    				if (n.l != null) {
    					q.add(n.l);
    				}
    				if (n.r != null) {
    					q.add(n.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;
    		}
    	}
    }
    
    

    此次学习感悟:

    在算法方面还较为薄弱,还需要更加的努力

    时间 链接
    2020.7.28 https://www.cnblogs.com/gaotian250yj912/p/13384552.html
    2020.8.1 https://www.cnblogs.com/gaotian250yj912/p/13412542.html
  • 相关阅读:
    求Mac 的adt插件!
    前端ajax异步传值以及后端接收参数的几种方式
    在eclipse中使用git clone 别人共享在Github的代码和上传到自己的仓库!
    Tomcat 配置虚拟路径保存、访问图片
    sssp-springmvc+spring+spring-data-jpa问题总结
    redis整合异常总结
    sssp-springmvc+spring+spring-data-jpa增删改查
    ssm+PageHelper实现分页查询
    微信小程序异常解析
    CentOS 7.4中firewall防火墙详解和配置以及切换为iptables防火墙
  • 原文地址:https://www.cnblogs.com/gaotian250yj912/p/13549264.html
Copyright © 2011-2022 走看看