码迷,mamicode.com
首页 > 其他好文 > 详细

20165231 第十周课下补做

时间:2018-05-06 16:16:56      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:堆栈   ret   xxxxxx   代码   value   lin   排序   输出   ref   

相关知识点

  • 创建一个空的链表
    List<Student> list = new LinkedList<Student>();
  • 向链表中添加新的结点
    list.add(new Student(XXXXXXXXXXXXXXX));
  • 删除结点
    list.remove("xxxxxxx");
  • 链表中数据的插入
    list.add("**");
  • 链表中数据的排序
    Collections.sort();
  • 将list中的元素按升序排序
    public static sort(List<E>list)

    课上习题补做

    习题2

代码:

import java.util.*;
class StudentSort implements Comparable {
    double d=0;
    String s="";
    StudentSort (double d) {
        this.d=d;
    }
    StudentSort (String s) {
        this.s=s;
    }
    public int compareTo(Object b) {
        StudentSort st=(StudentSort)b;
        if((this.d-st.d)==0)
            return -1;
        else
            return (int)((this.d-st.d)*1000);
    }
}
class Student {
    String name=null;
    double math,english,computer,total,aver;
    Student(String s, double m, double e, double f, double a,double b) {
        name=s;
        math=m;
        english=e;
        computer=f;
        total=a;
        aver=b;
    }
}
public class StudentTest {
    public static void main(String args[]) {
        TreeMap<StudentSort, Student> treemap = new TreeMap<StudentSort, Student>();
        String str[] = {"20165229赵凯杰", "20165230田坤烨", "20165231王杨鸿永", "20165232何彦达", "20165233张雨昕"};
        double math[] = {89, 45, 78, 76, 70};
        double english[] = {67, 68, 69, 70, 71};
        double computer[] = {76, 66, 30, 65, 80};
        double total[] = new double[5];
        double aver[] = new double[5];
        Student student[] = new Student[5];
        for (int k = 0; k < student.length; k++) {
            total[k] = math[k] + english[k] + computer[k];
            aver[k] = total[k] / 3;
        }
        for (int k = 0; k < student.length; k++) {
            student[k] = new Student(str[k], math[k], english[k], computer[k], total[k], aver[k]);
        }
        StudentSort key[] = new StudentSort[5];
        for (int k = 0; k < key.length; k++) {
            key[k] = new StudentSort(student[k].total);
        }
        for (int k = 0; k < student.length; k++) {
            treemap.put(key[k], student[k]);
        }
        int number = treemap.size();
        System.out.println("有" + number + "个对象,按总成绩排序:");
        Collection<Student> collection = treemap.values();
        Iterator<Student> iter = collection.iterator();
        while (iter.hasNext()) {
            Student stu = iter.next();
            System.out.println("姓名 " + stu.name + " 总成绩 " + stu.total);
        }
    }
}

运行截图:
技术分享图片

代码码云

习题3

代码:

import java.util.*;
public class MyList {
    public static void main(String [] args) {
        List<String> list=new LinkedList<String>();
        list.add("20165229");
        list.add("20165230");
        list.add("20165232");
        list.add("20165233");
        System.out.println("打印初始链表");
        //把上面四个节点连成一个没有头结点的单链表
        Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.add("20165231");
        //把你自己插入到合适的位置(学号升序)
        System.out.println("插入我的学号后排序,打印链表");
        Collections.sort(list);
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.remove("20165231");
        //从链表中删除自己
        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 E15_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= new Integer (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 E15_2 {
    public static void main(String[] args) {
        List<CollegeStu> list=new LinkedList<CollegeStu>();
        int score []={65, 66, 88, 76, 77};
        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
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 E15_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+"元");
        }
    }
}

运行截图:
技术分享图片

20165231 第十周课下补做

标签:堆栈   ret   xxxxxx   代码   value   lin   排序   输出   ref   

原文地址:https://www.cnblogs.com/Yhooyon/p/8998289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!