呃,这次好尴尬,我发现自己对Java还不是一般的不了解。一个题目,暴露了好多问题。出题者的意图基本没get到,连内部类都是排序的时候才发现的。
一、难点
排序要求是先按总分从高到低排,如果总分相同则对比德分,按德分从高到低往下排,若德分也相同,则按准考证号大小从小到大往下排。
二、解决方案
失败的解决方案:我想用快速排序实现,但是发现用总分排序完以后没办法再往下细分了。简单地说就是,我掌握的快速排序只能按一个标准排序(我现在怀疑我到底有没有掌握快速排序)。昨天还觉得自己思路清晰,今天瞬间发现自己就是个蠢货。
成功的解决方案:实在是没办法了,又去百度了答案,这次答案来自错误版《福尔摩斯的约会》的约会。呃,这次其实也有点小错误,但是整体思路是对的。http://blog.sina.com.cn/s/blog_d06ce7ad0102v95g.html
三、最终答案
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Vector;
public class Main {
private class Student {
private String id;// 准考证
private int virtue;// 德分
private int ability;// 才分
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getVirtue() {
return virtue;
}
public void setVirtue(int virtue) {
this.virtue = virtue;
}
public int getAbility() {
return ability;
}
public void setAbility(int ability) {
this.ability = ability;
}
public int getSum(){
return this.getVirtue()+this.getAbility();
}
}
private class MyComparator1 implements Comparator {
public int compare(Object o1, Object o2) {
Student stu1 = (Student) o1;
Student stu2 = (Student) o2;
if (stu1.getSum() != stu2.getSum()) {
return stu2.getSum() - stu1.getSum();
} else if (stu1.getVirtue()!= stu2.getVirtue()) {
return stu2.getVirtue()- stu1.getVirtue();
} else
return stu1.getId().compareTo(stu2.getId());
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int m = s.nextInt();// 考生人数
int L = s.nextInt();// 录取线
int H = s.nextInt();// 优先录取线
Vector inV = new Vector();// 录取队列
Vector firstV = new Vector();// 德才兼备,德>=H,才>=H
Vector secondeV = new Vector();// 德胜才,德>=H,才<H
Vector thirdV = new Vector();// 才德兼亡但德胜才,德>=才,德<H,才<H
Vector fourV = new Vector();// 德<H,才<H,德<=才
for (int i = 0; i < m; i++) {
Main.Student student = new Main().new Student();
student.setId(s.next());
student.setVirtue(s.nextInt());
student.setAbility(s.nextInt());
if (student.getVirtue() >= L && student.getAbility() >= L) {
inV.addElement(student);
}
}
for (int i = 0; i < inV.size(); i++) {
Main.Student temp = new Main().new Student();
temp = (Student) inV.elementAt(i);
if (temp.getVirtue() >= H && temp.getAbility() >= H) {
firstV.addElement(temp);
}else
if (temp.getVirtue() >= H && temp.getAbility() < H) {
secondeV.addElement(temp);
}else
if (temp.getVirtue() < H && temp.getAbility() < H
&& (temp.getVirtue() >= temp.getAbility())) {
thirdV.addElement(temp);
}
else{
fourV.addElement(temp);
}
}
System.out.println(inV.size());
Comparator cm = new Main(). new MyComparator1();
Collections.sort(firstV, cm);
Collections.sort(secondeV, cm);
Collections.sort(thirdV, cm);
Collections.sort(fourV, cm);
for (int i = 0; i < firstV.size(); i++) {
Student student = (Student) firstV.elementAt(i);
System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility());
}
for (int i = 0; i < secondeV.size(); i++) {
Student student = (Student) secondeV.elementAt(i);
System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility());
}
for (int i = 0; i < thirdV.size(); i++) {
Student student = (Student) thirdV.elementAt(i);
System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility());
}
for (int i = 0; i < fourV.size(); i++) {
Student student = (Student) fourV.elementAt(i);
System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility());
}
}
}
四、总结
这个题目算是把我彻底暴露了。遇到如下问题:
1、不知道如何创建内部类对象。虽然找到了解决办法,但是并没有深入了解,羞愧。http://android.blog.51cto.com/268543/384844/
2、存储对象用Vector代替数组,按我的理解,其实就是一个存储空间的问题,一个长度是固定的,一个是可扩充的。好多人都用Vector,我相信群众的眼睛是雪亮的(其实是觉得自己太浅薄狭隘了)。
①简介:http://blog.csdn.net/m_leonwang/article/details/8507535
②详解:http://blog.csdn.net/yuliming5219/article/details/5655594
3、比较器Comparator的运用。Comparator可以进行复杂的排序,尤其是本题这种情况,完美适配。我刚刚才知道这个强大的接口。http://muscle-liu.iteye.com/blog/157261
五、无用的代码
我自己写的排序,舍不得删了,留念而已:
private void quickSort(int[] sort, int left, int right) {
int dp;
if (left < right) {
//第一次循环,通常不能保证排序正确,但是能保证左边的数全部小于temp,右边的数全部大于temp
dp = partition(sort, left, right);
System.out.println("partition(sort,"+left+", "+right+")结果为:");
for (int i = 0; i < 5; i++) {
System.out.print("sort[" + i + "]=" + sort[i]+" ");
}
System.out.println();
//由于第一次循环结束已经将序列分成了两部分,现在排序左边的部分
quickSort(sort, left, dp - 1);
System.out.println("quickSort(sort,"+ left+", "+(dp - 1)+")结果为(此时dp="+dp+"):");
for (int i = 0; i < 5; i++) {
System.out.print("sort[" + i + "]=" + sort[i]+" ");
}
System.out.println();
//排序右边的部分
quickSort(sort, dp + 1, right);
System.out.println("quickSort(sort, "+(dp + 1)+", "+right+")结果为(此时dp="+dp+"):");
for (int i = 0; i < 5; i++) {
System.out.print("sort[" + i + "]=" + sort[i]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
int sort[] = { 20,40,50,10,60};
System.out.println("原始数据:");
for (int i = 0; i < 5; i++) {
System.out.print("sort[" + i + "]=" + sort[i]+" ");
}
Quick q=new Quick();
q.quickSort(sort, 0, 4);
}
搜索
复制