生活随笔
收集整理的這篇文章主要介紹了
C~K要找女朋友了!!!_JAVA
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
臨近11.11,CK看見周圍的朋友一個(gè)個(gè)的都脫單或者正準(zhǔn)備脫單了,CK也想要找一個(gè)女朋友了(聽說國家會(huì)分配?)。MeiK聽說了這件事情,表
示CK終于開悟了,所以他整理了一份候選人名單給CK??墒荂~K心里有自己心動(dòng)女生的身高區(qū)間和年齡限制,所以他想把符合條件的女生
的信息(即符合[身高最小值,身高最大值]閉區(qū)間和[年齡最小值,年齡最大值] 閉區(qū)間的女生都算符合條件)給篩選出來,但是這可是難住了CK,事關(guān)CK的幸福,你能幫幫他嗎?
ps:由于MeiK比較傻,所以名單里可能會(huì)有重復(fù)的女生的信息,若信息重復(fù),則第一次輸入為有效信息。
Input
第一行輸入MeiK的候選人名單里有N個(gè)人(N<100000)。
第二行輸入四個(gè)整數(shù)a,b,c,d。分別表示C~K心動(dòng)女生的身高的最小值和最大值,年齡的最小值和最大值。(題目保證a<=b,c<=d)
接下來輸入N行,每行表示一個(gè)女生的信息(姓名,身高,年齡,聯(lián)系方式)
ps:聯(lián)系方式不超過11個(gè)字符。
Output
第一行輸出一個(gè)n,表示符合條件的女生的數(shù)量。
接下來的n行,每一行輸出一個(gè)符合條件的女生的信息。
輸出順序按身高從低到高排序,若身高相同,則按年齡從高到底排序,若年齡也相同,則按照輸入順序輸出。
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();}
}
總結(jié)
以上是生活随笔為你收集整理的C~K要找女朋友了!!!_JAVA的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。