两个文件:info.txt 存放学生基本信息
学号 学院 专业 姓名
1001 计算机学院 软件工程 刘月
1002 生物工程 服装设计 孙丽
score.txt存放分数信息
学号 学科 成绩
1001 数据结构 85
1001 线性代数 89
1002 数据库 100
1002 面料工艺 30
求各学院各专业总分最高的学生。
首先分析,学号--专业是一对一的,一个学生只能属于一个专业,为简单起见, 对于并列第一名的学生,只取第一个。
Student.java
public class Student { private int id; private String school; private String major; private String name; public Student() { } public Student(int id,String school,String major,String name) { this.id = id; this.school = school; this.major = major; this.name = name; } public String toString(){ return this.id +" "+this.school +" "+ this.name+" "+this.major; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
PrintTheNo1.java
import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Set; public class PrintTheNo1 { public static void main(String [] args) throws IOException { try{ LinkedList<Student> stuList =getStuList(); HashMap<Integer,Double> map = getSumScoreOfStudent(); getTopStu(stuList,map); }catch(IOException io) { System.out.println("出错了呀!"+io); } } //读取info文件 public static LinkedList<Student> getStuList() throws IOException { String oneLine; try{ FileReader a = new FileReader("D:\NewPro\Files\info.txt"); BufferedReader br = new BufferedReader(a); int line = 0; LinkedList<Student> stuList = new LinkedList<Student>(); while((oneLine = br.readLine() )!= null) { //从文件的第二行开始读取数据 line++; if(line == 1) continue; else { String[] info = oneLine.split(" "); Student stu = new Student(Integer.parseInt(info[0]),(String)info[1],(String)info[2],(String)info[3]); stuList.add(stu); } } for(Student s:stuList) System.out.println(s.toString()); br.close(); return stuList; }catch(IOException io) { System.out.println("error@"+io); return null; } } //求各个学生的总分 public static HashMap<Integer,Double> getSumScoreOfStudent() throws IOException { HashMap<Integer,Double> map = new HashMap<Integer,Double>(); String oneLine; try{ FileReader a = new FileReader("D:\NewPro\Files\score.txt"); BufferedReader br = new BufferedReader(a); int line = 0; double sum = 0; while((oneLine = br.readLine() )!= null) { ++line; if(line == 1) continue; else { String [] info = oneLine.split(" "); int id = Integer.parseInt(info[0]); if(map.containsKey(id)) { sum = map.get(id)+Double.parseDouble(info[2]); map.put(id,sum); } else map.put(id,Double.parseDouble(info[2])); } } return map; }catch(IOException io) { System.out.println("error in score.txt"+io); return null; } } //各个专业第一名的学生 public static void getTopStu(LinkedList<Student> stuList,HashMap<Integer,Double> map) { Student s = new Student(); int id =0; double score = 0; double maxScore = 0; String major; //MajorAndScore保存专业--最高分 HashMap<String,Double> MajorAndScore = new HashMap<String,Double>(); //result保存 专业--学号,然后遍历result,根据result的学号值找到Student信息,保存到topStudent链表中。 HashMap<String,Integer> result = new HashMap<String,Integer>(); LinkedList<Student> topStudent = new LinkedList<Student>(); if(stuList.size() == 0 || map.size() ==0 ) return; for(int i=0;i<stuList.size();i++) { s = stuList.get(i); id =s.getId(); score = map.get(id);//拿到该学生的总分 major = s.getMajor(); if(map.containsKey(id) ) { if(MajorAndScore.containsKey(major) && score > maxScore) //如果已经有major.且当前学生的总分更大一些,把更大的分数加进去 { maxScore = score; MajorAndScore.put(major,maxScore); result.put(major,id); } else if(!MajorAndScore.containsKey(major)) //如果不存在major key { MajorAndScore.put(major,score); result.put(major,id); } else { map.remove(id); } } } @SuppressWarnings("rawtypes") Set st = result.entrySet(); Iterator it =st.iterator(); while(it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); topStudent.add(getById(stuList,(Integer)entry.getValue())); } System.out.println( "各专业第一名是: "); for(int i=0;i<topStudent.size();i++) { System.out.println(topStudent.get(i).toString()); } } //根据id找到学生信息 private static Student getById(LinkedList<Student> stuList,int id) { for(int i=0;i<stuList.size();i++) { if(stuList.get(i).getId() == id) { return stuList.get(i); } } return null; } }