167 - 学生列表
Time Limit: 1000 Memory Limit: 65535
Submit: 93 Solved: 53
Description
编写学生类,包含学号no、姓名name、成绩score,提供必要的构造函数、toString函数和equals/hashcode函数,其中,toString函数的格式为“no:xxx name:xxx score:xxx”,no参与equals和hashcode的计算
在main函数中构造一个学生列表对象(List),用于存放多个学生对象
从命令行输入多个学生对象,存入列表中
从命令行中读入在列表对象上的操作,具体操作包含:
add 添加一个学生(包含学号和学生姓名)
delete 删除一个学生(包含学号)
set 修改一个学生信息(只修改某学号学生的成绩)
完成操作后按列表顺序输出集合中的学生
Input
学生个数
学生对象数据
操作数
操作内容
Output
列表顺序输出集合中的学生
Sample Input
4
1 wong 90
2 liu 80
3 chen 70
4 fang 60
3
add 5 duan 80
delete 3
set 4 70
Sample Output
no:1 name:wong score:90
no:2 name:liu score:80
no:4 name:fang score:70
no:5 name:duan score:80
_______________________________
import java.util.*;
public class Main {
public static void main(String[] args){
List
Scanner scan = new Scanner(System.in);
int num=scan.nextInt();
for(int i=0;i int no=scan.nextInt(); String name=scan.next(); int score=scan.nextInt(); list.add(new Student(no,name,score)); } int opnum=scan.nextInt(); for(int i=0;i String op=scan.next(); if(op.equals("add")) { int no=scan.nextInt(); String name=scan.next(); int score=scan.nextInt(); list.add(new Student(no,name,score)); } else if(op.equals("delete")) { int n=scan.nextInt(); for(int j=0;j if(list.get(j).no==n){ list.remove(j); } } } else if(op.equals("set")) { int pos=scan.nextInt(); int s=scan.nextInt(); for(int j=0;j if(list.get(j).no==pos){ list.set(j,new Student(list.get(j).no,list.get(j).name,s)); } } } } for(Student s:list) System.out.println(s.toString()); } } class Student { int no; String name; int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public String toString() { return "no:"+no+" name:"+name+" score:"+score; } public boolean equals(Object o) { if (o == null) return false; else { boolean result = false; if (o instanceof Student) { Student rec = (Student) o; if (this.no == rec.no&&this.name.equals(((Student) o).name)) { result = true; } } return result; } } public int hashcode() { int result = 17; // 任意素数 result = 31 * result + no; result = 31* result + name.hashCode(); return result; } } ########################################### 168 - 学生Map Time Limit: 1000 Memory Limit: 65535 Submit: 109 Solved: 76 Description 修改《学生列表》题目,使用学生Map来存放学生的集合,其中key为学号,value为学生对象 输出时按照学生的学号顺序输出 Input 学生个数 学生对象数据 操作数 操作内容 Output 按照学号顺序输出集合中的学生 Sample Input 4 1 wong 90 2 liu 80 3 chen 70 4 fang 60 3 add 5 duan 80 delete 3 set 4 70 Sample Output no:1 name:wong score:90 no:2 name:liu score:80 no:4 name:fang score:70 no:5 name:duan score:80 ___________________________ import java.util.*; public class Main { public static void main(String[] args){ Map Scanner scan = new Scanner(System.in); int num=scan.nextInt(); for(int i=0;i int no=scan.nextInt(); String name=scan.next(); int score=scan.nextInt(); map.put(no,new Student(no,name,score)); } int opnum=scan.nextInt(); for(int i=0;i String op=scan.next(); if(op.equals("add")) { int no=scan.nextInt(); String name=scan.next(); int score=scan.nextInt(); map.put(no,new Student(no,name,score)); } else if(op.equals("delete")) { int n=scan.nextInt(); map.remove(n); } else if(op.equals("set")) { int pos=scan.nextInt(); int s=scan.nextInt(); for(Student stu: map.values()){ if(stu.getNo()==pos) stu.setScore(s); } } } for(Student s:map.values()) System.out.println(s.toString()); } } class Student { int no; String name; int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} void setScore(int score) { this.score=score; } public String toString() { return "no:"+no+" name:"+name+" score:"+score; } public boolean equals(Object o) { if (o == null) return false; else { boolean result = false; if (o instanceof Student) { Student rec = (Student) o; if (this.no == rec.no&&this.name.equals(((Student) o).name)) { result = true; } } return result; } } public int hashcode() { int result = 17; // 任意素数 result = 31 * result + no; result = 31* result + name.hashCode(); return result; } } ########################################## 184 - 4 Time Limit: 1000 Memory Limit: 65535 Submit: 140 Solved: 40 Description 在上题的基础上构建一个书单类BookList,该类中用一个列表类对象存放书单,提供添加图书(addBook)、查找图书(searchBook)的函数 main函数从键盘输入多个Book添加到书单中,(添加时,提供书的名称、价格、作者、版本号),而后从键盘读入一本书,查找该列表对象中是否包含该书,若包含,输出”found: 该书在列表中的序号”,若不包含,输出“not found”,查找时,提供书的名称、作者、版本号。 Input 添加书的个数 添加的书 查找的书 Output 查找结果 Sample Input 2 ThinkingInJava 86 BruceEckel 4 CoreJava 95 CayS.Horstmann 10 CoreJava CayS.Horstmann 10 Sample Output found: 1 ________________________________________ import java.util.*; public class Main{ public static void main(String[] args) {d Scanner s = new Scanner(System.in); BookList bl = new BookList(); int n = s.nextInt(); for (int i=0; i bl.addBook(new Book(s.next(), s.nextInt(), s.next(), s.nextInt())); } bl.searchBook(new Book(s.next(), 0, s.next(), s.nextInt())); } } class Book{ String name; int price; String author; int no; Book(String name,int price,String author,int no){ this.name=name; this.price=price; this.author=author; this.no=no; } } class BookList{ List void addBook(Book b) { book.add(b); } void searchBook(Book B) { for(Book b:book) { if(b.name.equals(B.name)&&b.author.equals(B.author)&&b.no==B.no) { System.out.println("found: "+book.indexOf(b)); return ; } } System.out.println("not found"); } }