20165233 第十週課下補作

20165233 第十週課下補作

相關知識點總結&代碼與結果補充

課上習題2

  • 數據結構和算法中,兩種重要的排序方法:java

    • 有類的源代碼,針對某一成員變量排序,讓類實現Comparable接口,調用Collection.sort(List)
    • 沒有類的源代碼,或者多種排序,新建一個類,實現Comparator接口 調用Collection.sort(List, Compatator)
  • 建立一個空的鏈表
List<Student> list = new LinkedList<Student>();
  • 向鏈表中添加新的結點
list.add(new Student(5233,"張雨昕","female",20));
  • 代碼實現
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
public class ComparatorUse {
    public  static void main(String[] args) {
        List<Student> list = new LinkedList<>();
        list.add(new Student(5233,"張雨昕","female",20,80,67,89));
        list.add(new Student(5232,"何彥達","male",20,79,66,45));
        list.add(new Student(5235,"祁瑛","male",20,75,90,78));
        list.add(new Student(5231,"王楊鴻永","male",20,69,56,76));
        list.add(new Student(5234,"劉津甫","male",20,82,80,83));
        System.out.println("排序前的數據:");
        for (Student student : list) {
            System.out.println(student);
        }
        SortByID sortByID = new SortByID();
        Collections.sort(list, sortByID);
        System.out.println("根據學生學號由低到高排序:");
        for (Student student : list) {
            System.out.println(student);
        }
        SortByTotal_score sortBytotal_score = new SortByTotal_score();
        Collections.sort(list, sortBytotal_score);
        System.out.println("根據學生成績由低到高排序:");
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
class Student {

    private int id;//表示學號
    private String name;//表示姓名
    private int age;//表示年齡
    private String sex;
    private double computer_score;//表示計算機課程的成績
    private double english_score;//表示英語課的成績
    private double maths_score;//表示數學課的成績
    private double total_score;// 表示總成績
    private double ave_score; //表示平均成績

    @Override
    public String toString() {
        return "Student[name:"+name+",age:"+age+",number:"+id+",total_score"+total_score+"]";
    }

    public Student(int id, String name, String sex, int age,double computer_score,
                   double english_score,double maths_score) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.computer_score = computer_score;
        this.english_score = english_score;
        this.maths_score = maths_score;
    }

    public int getId() {
        return id;
    }//得到當前對象的學號,

    public double getComputer_score() {
        return computer_score;
    }//得到當前對象的計算機課程成績,

    public double getMaths_score() {
        return maths_score;
    }//得到當前對象的數學課程成績,

    public double getEnglish_score() {
        return english_score;
    }//得到當前對象的英語課程成績,

    public void setId(int id) {
        this.id = id;
    }// 設置當前對象的id值,

    public void setComputer_score(double computer_score) {
        this.computer_score = computer_score;
    }//設置當前對象的Computer_score值,

    public void setEnglish_score(double english_score) {
        this.english_score = english_score;
    }//設置當前對象的English_score值,

    public void setMaths_score(double maths_score) {
        this.maths_score = maths_score;
    }//設置當前對象的Maths_score值,

    public double getTotalScore() {
        total_score=computer_score + maths_score + english_score;
        return total_score;
    }// 計算Computer_score, Maths_score 和English_score 三門課的總成績。

    public double getAveScore() {
        return getTotalScore() / 3;
    }// 計算Computer_score, Maths_score 和English_score 三門課的平均成績。

}

class SortByID implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getId() - o2.getId();
    }

}
class SortByTotal_score implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (int)( o1.getTotalScore() - o2.getTotalScore());
    }
}
  • 結果截圖

課上習題3

  • 有關單鏈表的建立以及新結點的添加知識點,見上一道習題知識點總結。算法

  • 把結點連成一個沒有頭結點的單鏈表:鏈表中的結點是自動連接在一塊兒的,不須要咱們作連接,也就是說,不須要操做安排結點中所存放的下一個或上一個結點的引用。編程

  • 遍歷鏈表,利用迭代器
Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
  • 按照學號進行升序排序:利用public static sort(List<E>list)方法,能夠將list中的元素按升序排序。數據結構

  • 刪除結點
list.remove("20165233");
  • 代碼實現
import java.util.*;
public class MyList {
    public static void main(String [] args) {
        List<String> list=new LinkedList<String>();
        list.add("20165231");
        list.add("20165232");
        list.add("20165234");
        list.add("20165235");
        System.out.println("打印初始鏈表");
        //把上面四個節點連成一個沒有頭結點的單鏈表
        Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍歷單鏈表,打印每一個結點的
        list.add("20165233");
        //把你本身插入到合適的位置(學號升序)
        System.out.println("插入個人學號後排序,打印鏈表");
        Collections.sort(list);
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍歷單鏈表,打印每一個結點的
        list.remove("20165233");
        //從鏈表中刪除本身
        System.out.println("刪除個人學號後打印鏈表");
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍歷單鏈表,打印每一個結點的
    }
}
  • 運行結果截圖

第十五章課後編程題

第1題:

使用堆棧結構輸出an的若干項,其中an=2an-1+2an-2,a1=3,a2=8.

  • 代碼實現
import java.util.*;
public class Ep15_1 {
    public static void main(String[] args) {
        Stack<Integer> stack=new Stack<Integer>();
        stack.push(3);
        stack.push(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= 2 * f1 + 2 * f2;
                System.out.println(""+temp.toString());
                stack.push(temp);
                stack.push(F2);
                k++;
            }
        }
    }
}
  • 運行結果截圖

第2題:

將鏈表中的學生英語成績單存放到一個樹集中,使得按成績自動排序,並輸出排序結果

  • 代碼實現
import java.util.*;
class CollegeStu implements Comparable {
    int english=0;
    String name;
    CollegeStu(int english,String name) {
        this.name=name;
        this.english=english;
    }
    @Override
    public int compareTo(Object b) {
        CollegeStu stu=(CollegeStu)b;
        return (this.english-stu.english);
    }
}
public class Ep15_2 {
    public static void main(String[] args) {
        List<CollegeStu> list=new LinkedList<CollegeStu>();
        int score []={67, 66, 90, 56, 80};
        String name []={"王楊鴻永","何彥達","張雨昕","劉津甫","祁瑛"};
        for (int i=0;i<score.length;i++) {
            list.add(new CollegeStu(score[i],name[i]));
        }
        Iterator<CollegeStu> iter=list.iterator();
        TreeSet<CollegeStu> mytree=new TreeSet<CollegeStu>();
        while (iter.hasNext()) {
            CollegeStu stu=iter.next();
            mytree.add(stu);
        }
        Iterator<CollegeStu> te=mytree.iterator();
        while (te.hasNext()) {
            CollegeStu 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;
    }

    @Override
    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 Ep15_3 {
    public static void main(String[] args) {
        TreeMap<UDiscKey,UDisc> treeMap=new TreeMap<UDiscKey,UDisc>();
        int amount[]={1,2,4,8,16};
        double price[]={867,266,390,556};
        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+"元");
        }
    }
}
  • 運行結果截圖

相關文章
相關標籤/搜索