167 - 學(xué)生列表
Time Limit: 1000? Memory Limit: 65535
Submit: 93? Solved: 53
Description
編寫學(xué)生類竭钝,包含學(xué)號(hào)no洼哎、姓名name、成績score躬充,提供必要的構(gòu)造函數(shù)口糕、toString函數(shù)和equals/hashcode函數(shù)譬胎,其中,toString函數(shù)的格式為“no:xxx name:xxx score:xxx”,no參與equals和hashcode的計(jì)算
在main函數(shù)中構(gòu)造一個(gè)學(xué)生列表對(duì)象(List)爬立,用于存放多個(gè)學(xué)生對(duì)象
從命令行輸入多個(gè)學(xué)生對(duì)象钾唬,存入列表中
從命令行中讀入在列表對(duì)象上的操作,具體操作包含:
add 添加一個(gè)學(xué)生(包含學(xué)號(hào)和學(xué)生姓名)
delete 刪除一個(gè)學(xué)生(包含學(xué)號(hào))
set 修改一個(gè)學(xué)生信息(只修改某學(xué)號(hào)學(xué)生的成績)
完成操作后按列表順序輸出集合中的學(xué)生
Input
學(xué)生個(gè)數(shù)
學(xué)生對(duì)象數(shù)據(jù)
操作數(shù)
操作內(nèi)容
Output
列表順序輸出集合中的學(xué)生
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<Student> list=new LinkedList<Student>();
? ? ? ? Scanner scan = new Scanner(System.in);
? ? ? ? int num=scan.nextInt();
? ? ? ? for(int i=0;i<num;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<opnum;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<num;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<num;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; // 任意素?cái)?shù)
? ? result = 31 * result + no;
? ? result = 31* result + name.hashCode();
? ? return result;
? ? }
}
###########################################
168 - 學(xué)生Map
Time Limit: 1000? Memory Limit: 65535
Submit: 109? Solved: 76
Description
修改《學(xué)生列表》題目侠驯,使用學(xué)生Map來存放學(xué)生的集合抡秆,其中key為學(xué)號(hào),value為學(xué)生對(duì)象
輸出時(shí)按照學(xué)生的學(xué)號(hào)順序輸出
Input
學(xué)生個(gè)數(shù)
學(xué)生對(duì)象數(shù)據(jù)
操作數(shù)
操作內(nèi)容
Output
按照學(xué)號(hào)順序輸出集合中的學(xué)生
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<Integer,Student> map = new HashMap<Integer,Student>();
? ? ? ? Scanner scan = new Scanner(System.in);
? ? ? ? int num=scan.nextInt();
? ? ? ? for(int i=0;i<num;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<opnum;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; // 任意素?cái)?shù)
? ? result = 31 * result + no;
? ? result = 31* result + name.hashCode();
? ? return result;
? ? }
}
##########################################
184 - 4
Time Limit: 1000? Memory Limit: 65535
Submit: 140? Solved: 40
Description
在上題的基礎(chǔ)上構(gòu)建一個(gè)書單類BookList吟策,該類中用一個(gè)列表類對(duì)象存放書單儒士,提供添加圖書(addBook)、查找圖書(searchBook)的函數(shù)
main函數(shù)從鍵盤輸入多個(gè)Book添加到書單中檩坚,(添加時(shí)着撩,提供書的名稱、價(jià)格匾委、作者拖叙、版本號(hào)),而后從鍵盤讀入一本書赂乐,查找該列表對(duì)象中是否包含該書薯鳍,若包含,輸出”found: 該書在列表中的序號(hào)”挨措,若不包含挖滤,輸出“not found”崩溪,查找時(shí),提供書的名稱壶辜、作者悯舟、版本號(hào)。
Input
添加書的個(gè)數(shù)
添加的書
查找的書
Output
查找結(jié)果
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<n;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<Book> book=new ArrayList<Book>();
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");
}
}