猜夫妻:
'EEIhO>Q< (1)4对夫妇中,王结婚时,周送礼了;
/!iLC3.F~ (2)周和钱是同一个排球队的队员;
7N<}yYah (3)李的爱人是陈的爱人的表哥;
W|^ u?s 3 (4)陈夫妇与邻居吵架,徐,周,吴的爱人都去助战;
EDdA-tdW (5)李,徐,周结婚前住一间集体宿舍。
F_^|c; 试求(1) 王,周,钱,陈,李,徐,吴,孙8人谁是男的?谁是女的?
)NNs7uN/ (2)谁和谁是夫妇?
J+h `Hj S@ P_eG'aI v 本次先做第一问,第二问我还在写,等出来后在帖上:
"wc*s#9 J7 {l'P,z import java.lang.*;
wM RVLU/ public class GuessCouple1{
5W )7~CYj /**数据成员*/
cZ>\^1.? Person[] sortBySpouse1=new Person[8];
4J"}XSN Person[] sortBySpouse2=new Person[8];
|b GY,k int x=0;int y=0;
Ly`qXT1 Person[] sortByEqualSex1=new Person[8];
$fy!vP3 Person[] sortByEqualSex2=new Person[8];
e_=<wbpom int n=0;int p=0;int b=0;
<[hIk_ Person[] woman=new Person[4];
!CBv6 X Person[] man=new Person[4];
@8]Sb|A{ int k=0;int s=0;int r=0;
Hn)#| 82# boolean sign=true;
YH6A+~'^ boolean sign1=true;
?WPt"lP boolean sign2=true;
~q@_^q `> x_v&Ks|) /**内部类*/
`X.tW|/t& public class Person{
(Yz}6 _Is\s private String name;
Sw.nIVw private boolean sex;
+.9`q J private Person spouse;
P#'g@O[{ Mfj<;S public Person(String name){
//ab*k this.name=name;
w 5LQ[% this.sex=false;
j[jtAWL this.spouse=null;
kdS#|]ss }
-U$lVoHg public Person( ){
C.U9(DfA/5 name=null;
B 7@'- sex=false;
KP=as89+h spouse=null;
nkWVxt }
X&[7(AY yZ87Z5P" public Person(String name,boolean sex,Person spouse){
G^_1VFAM this.name=name;
^.@Q#xh this.sex=sex;
2N1<2~y this.spouse=spouse;
"s2%7+%y lj-(/0&> }
Dv F7Aw n /LR E>$4 public void setName(String name){
]LbE\q), this.name=name;
Y"igJd) }
1Ue8r. B87w5]@( public String getName( ){
M8j}W_E7 return name;
7X]\!)XL }
+)N`Bm ?+X3a),` public void setSex(boolean sex){
8k *HeL'/ this.sex=sex;
H|]cUqw,u }
/=T!]H public boolean getSex( ){
P1Fy8W.X return sex;
41*d }
9It79C1 public void setSpouse (Person person1){
W8rVu! this.spouse=person1;
G={E hR2 }
/<Az9F2I% public Person getSpouse( ){
F-XxBJXx return spouse;
?[M5+J }
zGN/x }
Rqlqv[@ 6,7Eygj5<k /** 方法*/
MqJ:@*d public void isCouple(Person person1,Person person2 ){
Y3Ei@#~2 if(person1.getSpouse().getName()==person2.getName()) {
1@]{RD# person1.setSex(!person2.getSex());
1H(?vM*R person2.setSex(!person1.getSex());
Dd= vN85 System.out.println(person1.getName()+"'s sex and "+person2.getName()+"'s sex is not equal" );
WNrAd R'5 }
B~T]I;/y }
&*PPK(%$n >m_P5Wwg /<CUAGS4 public void marriage(Person person1,boolean person1Marriage ,Person person2,boolean person2giveGiftPerson1 ){
p ;wmW if( person1Marriage&&person2giveGiftPerson1)
C1,G8ls8 if(!person1.getName().equals(person2.getName()))
]Fa4?>Q4 System.out.println(person1.getName()+ " and "+person2.getName()+" are not couple");
-QADU2( sortBySpouse1[x++]=person1;sortBySpouse2[y++]=person2;
[&gM;` }
yfQ`&6}pb {Z O9`KN '(ER1ABe |K!Z)u public void oneVolleyballGroup(boolean oneGroup,Person person1,Person person2 ){
{#!C6mE5 if(oneGroup&&person1.getSex()==person2.getSex())
3Qi32z_ System.out.println(person1.getName()+" and "+person2.getName()+" is the same sex");
basyGC sortByEqualSex1[n++]=person1;sortByEqualSex1[n++]=person2;b=n;
joe@+PaUG }
Jng?%g mJ8z!D-kr um'c}og{ cJg ':[I public void liveCollectivityDorm(boolean liveTogether,Person person1,Person person2,Person person3 ){
WHY=o.'s if(liveTogether&&!person1.getName().equals(person2.getName( ))&&!person1.getName().equals(person3.getName())&&!person2.getName().equals(person3.getName())){
k@J)=<vO System.out.println(person1.getName()+"'s sex and "+person2.getName()+"'s sex is equal "+person3.getName()+"'s sex is equal");
[]z!!b[/ sortByEqualSex2[p++]=person1;sortByEqualSex2[p++]=person2;sortByEqualSex2[p++]=person3;
gMZ~N~x }
r"WiI< }
~yv fi"h TZ$j6{< 0vR0`-% public void isbrotherInLaw(Person person1,Person person2,boolean p1SIsBrotherInLawP2S ){
;EokN H?c& if(!person1.getName().equals(person2.getName())&& p1SIsBrotherInLawP2S){
LD#,$4B6 person1.setSex(false);
FE System.out.println(person1.getName()+" is a woman");
To/Tjt woman[k++]=person1;r=k;
"f)D@ }
dKCJ:TsE System.out.println(person1.getName()+"'s sex is woman and "+person1.getName()+"'s spouse is man");
h|Y)h1H$ }
oSp#HXyS;{ vX zRA$ x*=# cL*JK public void assistInFighting (boolean cCoupleQuarrel,Person cCouple,Person person1,Person person2,Person person3){
+}!C7L~ Q1 if (cCoupleQuarrel )
cHg5 ]dK- if(!cCouple.getName().equals(person1.getName( ))&&!cCouple.getName().equals(person2.getName( ))&&!cCouple.getName().equals(person3.getName( ))
o]7H /eGw0 &&!person1.getName().equals(person2.getName( ))&&!person1.getName().equals(person3.getName( ))&&!person2.getName().equals(person3.getName( ))){
a ?m9x 'r)ld}c ~zfE$@ System.out.println(cCouple.getName()+"'s spouse is not "+person1.getName()+" and "+cCouple.getName()+"'s spouse is not "+person1.getName()+" 's spouse");
Ko6nBV:V System.out.println(cCouple.getName()+"'s spouse is not "+person2.getName()+" and "+cCouple.getName()+"'s spouse is not "+person2.getName()+" 's spouse");
U,.h}l b System.out.println(cCouple.getName()+"'s spouse is not "+person3.getName()+" and "+cCouple.getName()+"'s spouse is not "+person3.getName()+" 's spouse");
o!K^c7BIr }
%86}yiUFZ6 sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person1;
O,1*iQ6 sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person2;
v8M^SFY sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person3;
&4-8vw9V }
EW0H1Ex >3%{ *% /{DS)ot| public void guessCoupleByRules( ){
~" Pecz /**事实数据*/
) EvOB Person[] person=new Person[8];
G'V$$Y* person[0]=new Person("wang",false,person[0]);
y*sUt2l` person[1]=new Person("chen",false,person[1]);
f{ePhQI_ person[2]=new Person("zhou",false,person[2]);
iybq3(. RR person[3]=new Person("qian",false,person[3]);
[=#xEHl} person[4]=new Person("wu",false,person[4]);
);}w^b3S$[ person[5]=new Person("sun",false,person[5]);
X Yt#_We person[6]=new Person("li",false,person[6]);
O1 Pf=h person[7]=new Person("xu",false,person[7]);
BWBXGU /** 调用方法*/
Dzgv,<z" marriage(person[0],true ,person[2],true );
ms5CI6Ud oneVolleyballGroup(true,person[2],person[3] );
($s isbrotherInLaw(person[6],person[1],true );
#vj i*7 assistInFighting (true,person[1],person[7],person[2],person[4]);
y4=zQg^ liveCollectivityDorm(true,person[6],person[7],person[2]);
5HmaK9_H/ /**计算*/
Q"4;!MmE' for(int v=0;v<b;v++)
\`O. y%B for(int t=0;t<p;t++){
vpgtt(4 X if(sortByEqualSex1[v].getName().equals(sortByEqualSex2[t].getName())){
(eG| kKt for(int z=0;z<p;z++){
-RE,gn for(int j=0;j<b;j++){
U/Pk- F4q if(!sortByEqualSex2[z].getName().equals(sortByEqualSex1[j].getName())) ;
oZJ{41e else{sortByEqualSex1[j]=sortByEqualSex2[z];sign2=false;}
G2wTR6#} }
zIoWz%$!G if(sign2) sortByEqualSex1[n++]=sortByEqualSex2[z];
a]BbGS sign2=true;
u?<LpZs6 }
|sV)bIsZ }
5mM#W:r }
]2oTcc 6 for(int m=0;m<r;m++)
&(vM?+! for(int q=0;q<n;q++){
@ZP%T_( if(woman[m].getName().equals(sortByEqualSex1[q].getName())) {
54=, Z. for(int l=0;l<n;l++){
?pSXOTFC8 for(int j=0;j<r;j++){
Vp"Z)K; if(!sortByEqualSex1[l].getName().equals(woman[j].getName())) ;
Mt@bi else { woman[j]=sortByEqualSex1[l];sign=false;}
!iDm 2& }
5{&B.&fQ if(sign) woman[k++]=sortByEqualSex1[l];
gK{ < sign=true;
ul#d"y? }
=B4Ck6( }
iH~!00a }
#^ rp$`Vd r=k;
Hkk= Yu< if(r==4) {
SjMra [mT System.out.println("These person's sex is partitioning");
S+Ho m System.out.println("The woman is");
-QLX.klCj for(int i=0;i<r;i++)
vYl\zKLgL System.out.println(woman
.getName()+" "); YITJqUc0
for(int j=0;j<8;j++){ h(*LL=V!g
int w=0; WUJa/
for(int i=0;i<r;i++){ {xjhVCb
if(!person[j].getName().equals(woman.getName())) w++; vH7'-yhw
} E|HN9B]
if(w==r) man[s++]=person[j]; !%N y_O
} W3&ixM
(XzRJ760N
System.out.println("The man is"); yszw_
for(int i=0;i<s;i++) &AIH5ZX4
System.out.println(man.getName()+" "); 0yv 2h U
} >Rt`|OG2y
} n81z)KS
yANZ<6 w
vWWk E-7K
public static void main( String[] args){ BCUUhuz
GuessCouple1 guessCouple=new GuessCouple1( ); `<LQ rNI0
guessCouple.guessCoupleByRules(); _(3=Gno]c
} -aSP7
} {F(???X?
eV+CsA+3%
sT3D6ToW
=Wwl HcX}'
21#@qHG
文章来源:http://www.javadby.com java大本营,java实力站点,更新速度快,内容全面,鼓励原创,吸引了相当数量的IT学习者。 欢迎加入java技术交流群41970496,共同进步。 It[h(Klu