美文网首页
2018-物联网Java期末考试

2018-物联网Java期末考试

作者: V0W | 来源:发表于2018-11-08 21:51 被阅读0次

238 - 数字统计

Description
输入一个长整型的数,统计其中0、1、2、3、4、5、6、7、8、9各个数字的个数,并将结果合成一个整数。(前面的0不输出)

Input
长整型数

Output
合成后的整数

Sample Input
234353632

Sample Output
24111000

MyAnswer

import java.util.*;
//H7EaGRl7
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long num = scan.nextLong();
        System.out.println(num);
        int a[] = {0,0,0,0,0,0,0,0,0,0};
        String s = String.valueOf(num);
        System.out.println(s.charAt(0));
        for(int i=0; i<10; i++)
            if (s.charAt(i) == '0') {
                a[0]++;
            } else if (s.charAt(i) == '1') {
                a[1]++;
            } else if (s.charAt(i) == '2') {
                a[2]++;
            } else if (s.charAt(i) == '3') {
                a[3]++;
            } else if (s.charAt(i) == '4') {
                a[4]++;
            } else if (s.charAt(i) == '5') {
                a[5]++;
            } else if (s.charAt(i) == '6') {
                a[6]++;
            } else if (s.charAt(i) == '7') {
                a[7]++;
            } else if (s.charAt(i) == '8') {
                a[8]++;
            } else if (s.charAt(i) == '9') {
                a[9]++;
            }
        long res=0;
        for(int i=0; i<10; i++){
            res = res*10 + i;
        }

        System.out.println(res);
    }

}

239 - 家具类

Description
构建家具类Furniture,包括长、宽、高,均为整数(cm)。提供相应的构造函数和get、set函数。
Main函数里构造家具对象,并调用相应的函数。

Input
家具对象的长宽高

Output
家具对象的相关属性

Sample Input
50 60 100

Sample Output
100
50
60

Pre Append Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Furniture f = new Furniture(sc.nextInt(),sc.nextInt(),sc.nextInt());
        System.out.println(f.getHeight());
        System.out.println(f.getLength());
        System.out.println(f.getWidth());
    }
}

MyAnswer


class Furniture{
    int length;
    int width;
    int height;
    Furniture(int length, int width, int height){
        this.length = length;
        this.width = width;
        this.height = height;
    }
    int getHeight(){
        return this.height;
    }
    int getWidth(){
        return this.width;
    }
    int getLength(){
        return this.length;
    }

}

241 - 手机类

Description
构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,GB为单位)、价格(整数)。提供带参数的构造函数,重写其equals方法,使得两个相同配置(型号、内存、存储相同即可,价格可不同)的手机为相等的手机。重写其toString函数,打印手机的配置信息,形式为CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx]
main函数中从键盘读入两个手机对象,比较他们是否相等,输出他们的配置信息。

Input
两个计算机对象,包含型号、内存、存储空间、价格

Output
两个对象是否相等,两个对象的配置信息

Sample Input
P20 8 64 4999
P20 8 64 4999

Sample Output
true
CellPhone [model:P20, memory:8, storage:64, price:4999]
CellPhone [model:P20, memory:8, storage:64, price:4999]

Pre Append Code

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        CellPhone c1 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
        CellPhone c2 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());

        System.out.println(c1.equals(c2));
        System.out.println(c1);
        System.out.println(c2);
    }
}

MyAnswer

class CellPhone{
    String model;
    int memory;
    int storage;
    int price;
    CellPhone(String model, int memory, int s, int p){
        this.model = model;
        this.memory = memory;
        this.storage = s;
        this.price = p;
    }

    public String toString(){
        String s = "CellPhone [model:"+ model+", memory:"+memory+", storage:"+storage+", price:"+price+"]";
        return s;
    }

    public boolean equals(CellPhone o) {
        if (this.model.equals(o.model) && this.memory == o.memory && this.storage==o.storage) {
            return true;
        }
        return false;
    }


}


242 - 租车服务

Description
某租车公司提供租车服务,针对不同的车辆类型,日租金的计算方式不同,具体地,对于货车而言,根据载重量load(单位是吨)计算,公式为load1000;对于大型客车而言,根据车内座位数seats计算,公式为seats50;对于小型汽车而言,根据车辆等级和折旧年数计算,公式为200*level/sqrt(year),其中sqrt表示平方根。设计合适的类继承结构实现上述功能,构造租车公司类CarRentCompany,提供静态函数rentVehicles,能够给定一组待租车辆,计算日租金总额。
在main函数中,读入多个车辆数据,并计算总的日租金。

Input
汽车数量
汽车种类 该类汽车相关属性
其中1表示货车,2表示大型客车,3表示小型汽车

Output
总的日租金,保留两位小数

Sample Input
3
1 3
2 50
3 5 5

Sample Output
5947.21

Pre Append Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int c = sc.nextInt();
        Vehicle[] vs = new Vehicle[c];
        for (int i=0;i<c;i++) {
            int type = sc.nextInt();
            Vehicle v = null;
            if (type == 1) {//货车
                vs[i] = new Truck (sc.nextDouble());
            } else if (type == 2) {
                vs[i] = new Keche(sc.nextInt());
            } else if (type == 3) {
                vs[i] = new Car(sc.nextInt(), sc.nextInt());
            }
        }
        
        System.out.printf("%.2f",CarRentCompany.rentVehicles(vs));
        
    }
    

}

MyAnswer

abstract class Vehicle{
    public abstract double getrent();
}

class Truck extends Vehicle{
    double load;
    Truck(double load){
        this.load = load;
    }
    public double getrent(){
        return load*1000;
    }
}

class Keche extends Vehicle{
    int seats;
    Keche(int seats){
        this.seats = seats;
    }
    public double getrent(){
        return seats*50;
    }
}

class Car extends Vehicle{
    int level;
    int year;
    Car(int level, int year){
        this.level = level;
        this.year = year;
    }

    public double getrent(){
        return 200*level/Math.sqrt(year);
    }
}

class CarRentCompany {
    public static double rentVehicles(Vehicle[] vs) {
        double rent = 0;
        for (Vehicle av : vs)
            rent += av.getrent();
        return rent;
    }
}

243 - 字符串加密

Description
尝试构造一种自定义的字符串加密方式,首先该字符串的长度对5求余加1作为种子数字,以该种子数字为间隔,获取原字符串的子字符序列,该序列求逆得到最终的序列。

Input
原字符串

Output
加密后的字符串

Sample Input
abcdefghijklmn

Sample Output
kfa

MyAnswer

import java.util.Scanner;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int len = s.length();
        int seed = len % 5 + 1;
        char[] ch = s.toCharArray();
        String newstr = "";
        for(int i=0; i<len; i += seed){
            newstr += ch[i];
        }
        System.out.println(new StringBuffer(newstr).reverse());
    }



}

244 - 成绩管理系统

Description
构造一个成绩管理系统CourseManagementSystem,该系统包括如下几个方法:void add(int no, int grade)添加该学号的成绩,如果系统已有该学生成绩,则输出"the student already exists";void delete(int no)删除某学号成绩,如果不存在此学生则输出"no such student";int query(int no)查询并返回该学号的成绩;统计成绩void statistics( )统计[0-59]、[60-69]、[70-79]、[80-89]、[90-100]各成绩段的学生个数并打印。请选择合适的容器实现上述功能。(题目假设不会重复添加相同学号的学生成绩)
main函数中读入操作类型及相关参数,并调用statictic函数输出学生成绩统计信息。

Input
操作个数
操作名 操作参数

Output
查询学生的成绩
各成绩段的学生个数

Sample Input
8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90

Sample Output
the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1

Pre Append Code

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        CourseManagementSystem cms = new CourseManagementSystem();
        int ops = sc.nextInt();
        for (int i=0;i<ops;i++) {
            String op = sc.next();
            if (op.equals("add")) 
                cms.add(sc.nextInt(), sc.nextInt());
            else if  (op.equals("delete"))
                cms.delete(sc.nextInt());
            else if  (op.equals("query")) {
                int no = sc.nextInt();
                int s = cms.query(no);
                System.out.println("the score for "+no+" is : "+s);
            }
        }
        cms.statistic();
    }
}

MyAnswer

/**
* 本题更好的做法是使用STL : Map
*/

class CourseManagementSystem{
    ArrayList<Student> ALs = new ArrayList<>();

    void add(int no, int grade){
        for(Student st: ALs){
            if(st.no == no){
                System.out.println("the student already exists");
                return ;
            }
        }
        Student stu = new Student(no,grade);
        ALs.add(stu);
    }

    void delete(int no){
        int flag = 0;
        for(int j=0;j<ALs.size(); j++)
            if (ALs.get(j).getNo() == no) {
                ALs.remove(j);
                flag = 1;
            }
        if(flag == 0){
            System.out.println("no such student");

        }
    }

    int query(int no){
        for(Student stu: ALs)
            if(stu.no == no) {
                return stu.getGrade();
            }
        return 0;
    }

    void statistic(){
        int a[] = {0,0,0,0,0};
        for(Student stu: ALs){
            if(stu.grade >=0&&stu.grade <=59)
                a[0]++;
            else if(stu.grade >=60&&stu.grade <=69)
                a[1]++;
            else if(stu.grade >=70&&stu.grade <=79)
                a[2]++;
            else if(stu.grade >=80&&stu.grade <=89)
                a[3]++;
            else if(stu.grade >=90&&stu.grade <=100)
                a[4]++;
        }
        System.out.println("[0-59] : "+a[0]);
        System.out.println("[60-69] : "+a[1]);
        System.out.println("[70-79] : "+a[2]);
        System.out.println("[80-89] : "+a[3]);
        System.out.println("[90-100] : "+a[4]);

    }
}



class Student{
    int no;
    int grade;   //根据题目描述,应该是int,实际情况下double更合适
    Student(int no,int score){
        this.no = no;
        this.grade = score;
    }
    int getNo(){return this.no;}
    int getGrade(){return this.grade;}
}

相关文章

网友评论

      本文标题:2018-物联网Java期末考试

      本文链接:https://www.haomeiwen.com/subject/wsugxqtx.html