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

第十周作业补做

时间:2018-05-06 23:30:08      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:技术分享   学生   u盘   java   ber   size   运行   http   str   

20165326第十周课上测试补做

知识点简介

课上代码

1
2

ch15代码分析

ch15课后习题

(1)使用堆栈结构输出an的若干项,其中a_n=2a_n-1+2a_n=2a_(n-1)+2a_(n-2),a_1=3,a_2=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(F2);
                k++;
            }
        }
    }
}

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

import java.util.*;

class Student2 implements Comparable{
    int english = 0;
    String name;
    Student2(int english,String name){
        this.name = name;
        this.english = english;
    }
    @Override
    public int compareTo(Object b){
        Student2 st = (Student2)b;
        return (this.english-st.english);
    }
}
public class E2 {
    public static void main(String[] args) {
        List<Student2> list = new LinkedList<Student2>();
        int score[] = {98,100,99,96,95,94};
        String name [] = {"Re","Or","Ye","Gr","Bl","Pu"};
        for(int i=0;i<score.length;i++){
            list.add(new Student2(score[i],name[i]));
        }
        Iterator<Student2> iter = list.iterator();
        TreeSet<Student2> mytree = new TreeSet<Student2>();
        while(iter.hasNext()){
            Student2 stu = iter.next();
            mytree.add(stu);
        }
        Iterator<Student2> te = mytree.iterator();
        while (te.hasNext()){
            Student2 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;
    }
    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[] = {1,2,3,4,8,16,32,64,128,10};
        double price[] = {10,20,30,40,70,60,70,100,90,80};
        UDisc UDisc[] = new UDisc[10];
        for(int i=0;i<UDisc.length;i++){
            UDisc[i]= new UDisc(amount[i],price[i]);
        }
        System.out.println("按容量排序如下:");
        UDiscKey key[]= new UDiscKey[10];
        for(int i=0;i<key.length;i++){
            key[i] = new UDiscKey(UDisc[i].amount);
        }
        for(int i=0;i<UDisc.length;i++){
            treeMap.put(key[i],UDisc[i]);
        }
        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();
        System.out.println("按价格排序如下:");
        for(int i=0;i<key.length;i++){
            key[i] = new UDiscKey(UDisc[i].price);
        }
        for(int i=0;i<UDisc.length;i++){
            treeMap.put(key[i],UDisc[i]);
        }
        number = treeMap.size();
        collection = treeMap.values();
        iter = collection.iterator();
        while (iter.hasNext()){
            UDisc disc = iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
    }
}

运行结果
技术分享图片

第十周作业补做

标签:技术分享   学生   u盘   java   ber   size   运行   http   str   

原文地址:https://www.cnblogs.com/Czzzz/p/9000205.html

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