本篇博客是我借鉴了某位大神的博客,然后修改了一些地方,由于是几天看的,那个大神的博客网址我忘记了,多有得罪,请谅解。
学生信息的添加与查询
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
设计一个学生添加和查询的系统,从键盘读入学生的数据,然后再从屏幕显示出来。
Input
第一行有2个整数N和M,其中:N——学生数量,M——学生属性数量;
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)
Output
输出所有学生的属性及数据。(每行的列数据之间用‘\t’进行分隔)
Sample Input
5 4 id name birthday score 0001 Mike 1990-05-20 98.5 0002 John 1992-05-20 67 0003 Hill 1994-05-02 36.5 0004 Christ 1996-05-20 86.5 0001 Jack 1998-05-20 96
Sample Output
id:0001 name:Jack birthday:1998_5_20 score:96.0 id:0002 name:John birthday:1992_5_20 score:67.0 id:0003 name:Hill birthday:1994_5_2 score:36.5 id:0004 name:Christ birthday:1996_5_20 score:86.5
Hint
Source
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
class change{
public String date(String str) throws ParseException {
// TODO Auto-generated method stub
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy_M_d");
return df2.format(df1.parse(str));
}
}
class sdut
{
String id;
String name;
String birthday;
double score;
public String getid() {
return id;
}
public void setid(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public sdut(String id,String name, String birthday, double score) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.score = score;
}
change d = new change();
public String toString()
{
try {
return "id:"+id+"\tname:"+name+"\tbirthday:"+d.date(birthday)+
"\tscore:"+String.format("%.1f", score);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return birthday;
}
}
public class Main{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
cin.nextLine();
cin.nextLine();
Map<String,sdut>map = new TreeMap<String,sdut>();
for(int i = 0; i < n; i++)
{
String id = cin.next();
sdut S = new sdut(id, cin.next(),cin.next(),cin.nextDouble());
map.put(id, S);
}
Iterator<sdut> it = map.values().iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}