生活随笔
收集整理的這篇文章主要介紹了
C~K要找女朋友了!!!_JAVA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
Output
2
女神3 163 21 33333
女神2 167 20 22222
import java
.util
.ArrayList
;
import java
.util
.Collections
;
import java
.util
.Comparator
;
import java
.util
.List
;
import java
.util
.Scanner
;class Girl {String name
;int height
;int age
;String telephone
;public Girl(String name
, int height
, int age
, String telephone
) {super();this.name
= name
;this.height
= height
;this.age
= age
;this.telephone
= telephone
;}@Overridepublic int hashCode() {final int prime
= 31;int result
= 1;result
= prime
* result
+ age
;result
= prime
* result
+ height
;result
= prime
* result
+ ((name
== null
) ? 0 : name
.hashCode());result
= prime
* result
+ ((telephone
== null
) ? 0 : telephone
.hashCode());return result
;}@Overridepublic boolean equals(Object obj
) {if (this == obj
)return true;if (obj
== null
)return false;if (getClass() != obj
.getClass())return false;Girl other
= (Girl
) obj
;if (age
!= other
.age
)return false;if (height
!= other
.height
)return false;if (name
== null
) {if (other
.name
!= null
)return false;} else if (!name
.equals(other
.name
))return false;if (telephone
== null
) {if (other
.telephone
!= null
)return false;} else if (!telephone
.equals(other
.telephone
))return false;return true;}@Overridepublic String
toString() {return name
+ " " + height
+ " " + age
+ " " + telephone
;}}public class Main {public static void main(String
[] args
) {Scanner reader
= new Scanner(System
.in
);int n
= reader
.nextInt();int a
= reader
.nextInt();int b
= reader
.nextInt();int c
= reader
.nextInt();int d
= reader
.nextInt();List
<Girl> list
= new ArrayList<Girl>();while (n
-- > 0) {String name
= reader
.next();int height
= reader
.nextInt();int age
= reader
.nextInt();String telephone
= reader
.next();if (height
>= a
&& height
<= b
&& age
>= c
&& age
<= d
) {Girl girl
= new Girl(name
, height
, age
, telephone
);if (!list
.contains(girl
))list
.add(girl
);}}Collections
.sort(list
, new Comparator<Girl>() {@Overridepublic int compare(Girl o1
, Girl o2
) {if (o1
.height
== o2
.height
) {return o2
.age
- o1
.age
;} else {return o1
.height
- o2
.height
;}}});System
.out
.println(list
.size());for (Girl girl
: list
) {System
.out
.println(girl
);}reader
.close();}
}
總結
以上是生活随笔為你收集整理的C~K要找女朋友了!!!_JAVA的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。