Problem Description
临近11.11,CK看见周围的朋友一个个的都脱单或者正准备脱单了,CK也想要找一个女朋友了(听说国家会分配?)。MeiK听说了这件事情,表
示CK终于开悟了,所以他整理了一份候选人名单给CK。可是C~K心里有自己心动女生的身高区间和年龄限制,所以他想把符合条件的女生
的信息(即符合[身高最小值,身高最大值]闭区间和[年龄最小值,年龄最大值] 闭区间的女生都算符合条件)给筛选出来,但是这可是难住了CK,事关CK的幸福,你能帮帮他吗?
ps:由于MeiK比较傻,所以名单里可能会有重复的女生的信息,若信息重复,则第一次输入为有效信息。
Input
第一行输入MeiK的候选人名单里有N个人(N<100000)。
第二行输入四个整数a,b,c,d。分别表示C~K心动女生的身高的最小值和最大值,年龄的最小值和最大值。(题目保证a<=b,c<=d)
接下来输入N行,每行表示一个女生的信息(姓名,身高,年龄,联系方式)
ps:联系方式不超过11个字符。
Output
第一行输出一个n,表示符合条件的女生的数量。
接下来的n行,每一行输出一个符合条件的女生的信息。
输出顺序按身高从低到高排序,若身高相同,则按年龄从高到底排序,若年龄也相同,则按照输入顺序输出。
Sample Input
4
160 170 20 22
女神1 161 19 11111
女神2 167 20 22222
女神2 167 20 22222
女神3 163 21 33333
Sample Output
2
女神3 163 21 33333
女神2 167 20 22222
AC代码:
import java.util.*;
class A {
String name;
int h, w;
String phone;
public A(String name, int h, int w, String phone) {
super();
this.name = name;
this.h = h;
this.w = w;
this.phone = phone;
}
@Override
public String toString() {
return name + " " + h + " " + w + " " + phone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + h;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
result = prime * result + w;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (h != other.h)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
if (w != other.w)
return false;
return true;
}
}
public class Main {
public static void main(String[] args) {
Scanner mi = new Scanner(System.in);
int n = mi.nextInt();
int h1 = mi.nextInt();
int h2 = mi.nextInt();
int w1 = mi.nextInt();
int w2 = mi.nextInt();
HashSet<A> set = new HashSet<A>();
while (n-- > 0) {
A a = new A(mi.next(), mi.nextInt(), mi.nextInt(), mi.next());
if (a.h >= h1 && a.h <= h2 && a.w >= w1 && a.w <= w2) {
set.add(a);
}
}
ArrayList<A> list = new ArrayList<A>();
Iterator<A> it = set.iterator();
while (it.hasNext()) {
list.add(it.next());
}
Collections.sort(list, new Comparator<A>() {
@Override
public int compare(A o1, A o2) {
// TODO Auto-generated method stub
if (o1.h == o2.h) {
return o2.w - o1.w;
}
return o1.h - o2.h;
}
});
Iterator<A> it1 = list.iterator();
System.out.println(list.size());
while (it1.hasNext()) {
System.out.println(it1.next());
}
mi.close();
}
}
——————
余生还请多多指教!