• 20165202 week10课下补做


    相关知识点总结

    在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:

    • 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
    • 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)
    • 泛型类声明:
      class 名称<泛型列表>
      
    • 创建链表
      LinkedList<String> mylist=new LinkedList<String>();
      
    • 向链表增加节点
      list.add(E obj);
      
    • 从链表中删除节点
      list.remove(index)
      
    • 升序排序
      public static sort(List<E>list)
      
    • 折半查找list是否含有和参数key一样的元素
      int binarySearch(List<T>,Tkey,compareTo<T>c)
      
    • 树映射 TreeMap<K,V>适合用于数据的排序
    • 通过关键字进行排序TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>()

    课上内容的补做及结果截图

    习题2:
    成绩排序已提交,学号排序忘记传了:

    习题3:

    • 代码实现:
    import java.util.*;
    class Stu implements Comparable{
        int id;
        String name;
        Stu(String n, int i){
            name=n;
            id=i;
        }
        public int compareTo(Object b){
            Stu st=(Stu)b;
            return (this.id-st.id);
        }
    }
    public class MyList {
        public static void main(String [] args) {
            //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
            LinkedList<Stu> list=new LinkedList<>();
            list.add(new Stu("其米仁增",5239));
            list.add(new Stu("李梓豪",5201));
            list.add(new Stu("夏云霄",5203));
            list.add(new Stu("贾普涵",5204));
            //把上面四个节点连成一个没有头结点的单链表
            Iterator<Stu> iter=list.iterator();
            //遍历单链表,打印每个结点的
            System.out.println("初始单链表为:");
            while (iter.hasNext()){
                Stu st=iter.next();
                System.out.println(st.id+" "+st.name);
            }
            //把你自己插入到合适的位置(学号升序)
            list.add(new Stu("贾海粟",5202));
            Collections.sort(list);
            //遍历单链表,打印每个结点的
            iter=list.iterator();
            System.out.println("插入我的学号和姓名后单链表为:");
            while (iter.hasNext()){
                Stu st=iter.next();
                System.out.println(st.id+" "+st.name);
            }
            //从链表中删除自己
            list.remove(1);
            iter=list.iterator();
            //遍历单链表,打印每个结点的
            System.out.println("删除我的学号和姓名后单链表为:");
            while (iter.hasNext()){
                Stu st=iter.next();
                System.out.println(st.id+" "+st.name);
            }
    
        }
    }
    
    • 运行截图

    教材第十五章的代码分析

    补做教材第十五章的编程题目

    1.使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8

    • 代码实现
    import java.util.*;
    public class E1 {
        public static void main(String args[]) {
            Stack<Integer> stack=new Stack<Integer>();
            stack.push(new Integer(3));
            stack.push(new Integer(8));
            int k=1;
            while(k<=10) {
                for(int i=1;i<=2;i++) {
                    Integer F1=stack.pop();
                    int f1=F1.intValue();
                    Integer F2=stack.pop();
                    int f2=F2.intValue();
                    Integer temp=new Integer(2*f1+2*f2);
                    System.out.println(""+temp.toString());
                    stack.push(temp);
                    stack.push(F2);
                    k++;
                }
            }
        }
    }
    
    • 结果截图

    2.编写一个程序:将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果

    • 代码实现
    import java.util.*;
    class Student implements Comparable {
        int english=0;
        String name;
        Student(int english,String name) {
            this.name=name;
            this.english=english;
        }
        public int compareTo(Object b) {
            Student st=(Student)b;
            return (this.english-st.english);
        }
    }
    public class E2 {
        public static void main(String args[]) {
            List<Student> list=new LinkedList<Student>();
            int score []={100,76,45,99,77,88,75,79};
            String name[]={"一文","李悦","江流","胡克","魏凡","周平","赵剑","魏派"};
            for(int i=0;i<score.length;i++){
                list.add(new Student(score[i],name[i]));
            }
            Iterator<Student> iter=list.iterator();
            TreeSet<Student> mytree=new TreeSet<Student>();
            while(iter.hasNext()){
                Student stu=iter.next();
                mytree.add(stu);
            }
            Iterator<Student> te=mytree.iterator();
            while(te.hasNext()) {
                Student stu=te.next();
                System.out.println(""+stu.name+" "+stu.english);
            }
        }
    }
    
    • 结果截图

    3.有10个U盘,有两个重要的属性:价格和容量,编写一个应用程序,使用TreeMap<K,V>类,分别按照价格和容量排序输出10个U盘的详细信息。

    • 代码实现
    import java.util.*;
    class UDiscKey implements Comparable {
        double key=0;
        UDiscKey(double d) {
            key=d;
        }
        public int compareTo(Object b) {
            UDiscKey disc=(UDiscKey)b;
            if((this.key-disc.key)==0)
                return -1;
            else
                return (int)((this.key-disc.key)*1000);
        }
    }
    class UDisc{
        int amount;
        double price;
        UDisc(int m,double e) {
            amount=m;
            price=e;
        }
    }
    public class E3 {
        public static void main(String args[ ]) {
            TreeMap<UDiscKey,UDisc>  treemap= new TreeMap<UDiscKey,UDisc>();
            int amount[]={2,4,8,16,32};
            double price[]={32,50,64,128};
            UDisc UDisc[]=new UDisc[4];
            for(int k=0;k<UDisc.length;k++) {
                UDisc[k]=new UDisc(amount[k],price[k]);
            }
            UDiscKey key[]=new UDiscKey[4] ;
            for(int k=0;k<key.length;k++) {
                key[k]=new UDiscKey(UDisc[k].amount);
            }
            for(int k=0;k<UDisc.length;k++) {
                treemap.put(key[k],UDisc[k]);
            }
            int number=treemap.size();
            Collection<UDisc> collection=treemap.values();
            Iterator<UDisc> iter=collection.iterator();
            while(iter.hasNext()) {
                UDisc disc=iter.next();
                System.out.println(""+disc.amount+"G "+disc.price+"元");
            }
            treemap.clear();
            for(int k=0;k<key.length;k++) {
                key[k]=new UDiscKey(UDisc[k].price);
            }
            for(int k=0;k<UDisc.length;k++) {
                treemap.put(key[k],UDisc[k]);
            }
            number=treemap.size();
            collection=treemap.values();
            iter=collection.iterator();
            while(iter.hasNext()) {
                UDisc disc=iter.next();
                System.out.println(""+disc.amount+"G "+disc.price+"元");
            }
        }
    }
    
    
    • 结果截图

    4.编程题目码云链接

  • 相关阅读:
    三维重建:SLAM算法的考题总结
    ubuntu16.04安装KDE
    pip更新
    DNN结构演进History—CNN-GoogLeNet :Going Deeper with Convolutions
    OpenCV直方图均衡化
    图像连通域检测的2路算法Code
    OpenCV中的模板匹配/Filter2d
    OpenCV边缘检测的详细参数调节
    ICCV2015上的GazeTracker论文总结
    图像的连通域检测的堆栈算法
  • 原文地址:https://www.cnblogs.com/jhs888/p/8997664.html
Copyright © 2020-2023  润新知