第一题ac 100 学生成绩排序问题
class Student{ //学生类 public String name; public int chinese; public int math; public int english; public int total; public int rank; public Student(String name,int chinese,int math,int english){ this.name = name; this.chinese = chinese; this.math = math; this.english = english; this.total = this.chinese+this.math+this.english; } //最终输出的结果 @Override public String toString(){ StringBuilder sb = new StringBuilder(); sb.append("rank:").append(rank).append(" "). append("name:").append(name).append(" "). append("total:").append(total).append(" "). append("chinese:").append(chinese).append(" "). append("math:").append(math).append(" "). append("english:").append(english); return sb.toString(); } } public class T1 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str; while((str = bf.readLine()) !=null){ Student stu[] = new Student[Integer.valueOf(str)]; String []data; String name; int chinese,math,english; //处理输入 for(int i=0;i<stu.length;i++){ data = bf.readLine().split(" "); name = data[0]; chinese = Integer.valueOf(data[1]); math = Integer.valueOf(data[2]); english = Integer.valueOf(data[3]); stu[i] = new Student(name,chinese,math,english); } //排序处理 重写Comparator Arrays.sort(stu, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { if(o1.total!=o2.total){//总分 return o2.total-o1.total; }else if(o1.chinese!=o2.chinese){ return o2.chinese-o1.chinese; }else if(o1.math != o2.math){ return o2.math-o1.math; }else if(o1.english!=o2.english){ return o2.english-o1.english; }else { return o1.name.compareTo(o2.name); } } }); Student pre = stu[0]; int rank = 1; stu[0].rank = rank; rank++; // 重点排序 for(int i=1;i<stu.length;i++){ if(stu[i].total!=pre.total||stu[i].chinese!=pre.chinese||stu[i].math!=pre.math||stu[i].english!=pre.english){ stu[i].rank = rank; }else{ //前后两个相同分数 stu[i].rank = pre.rank; } pre = stu[i]; rank++; } for(int i=0;i<stu.length;i++){ System.out.println(stu[i]); } } } }
第二题:
其实就是简单的if else判断
class Main { public static void main(String[] args) throws IOException { String str; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); while((str=bf.readLine())!=null){ if(str.equals("1")){ int count = 0; while((str = bf.readLine())!=null){ if(str.equals("") == false)//一定要判读是否为空 count++; } System.out.println(count); }else if(str.equals("Q")){ System.out.println("Quit"); }else { System.out.println("Wrong input,Please re-choose"); System.out.println("Menu Function Test"); System.out.println("1: Count Lines"); System.out.println("Q: Quit"); } } } }
全部评论
(2) 回帖