关闭

猜夫妻

502人阅读 评论(0) 收藏 举报

猜夫妻:
(1)4对夫妇中,王结婚时,周送礼了;
(2)周和钱是同一个排球队的队员;
(3)李的爱人是陈的爱人的表哥;
(4)陈夫妇与邻居吵架,徐,周,吴的爱人都去助战;
(5)李,徐,周结婚前住一间集体宿舍。
试求(1) 王,周,钱,陈,李,徐,吴,孙8人谁是男的?谁是女的?
        (2)谁和谁是夫妇?

 本次先做第一问,第二问我还在写,等出来后在帖上:

import java.lang.*;
public class GuessCouple1{
 /**数据成员*/
 Person[] sortBySpouse1=new Person[8];
 Person[] sortBySpouse2=new Person[8];
 int x=0;int y=0;
 Person[] sortByEqualSex1=new Person[8];
 Person[] sortByEqualSex2=new Person[8];
    int n=0;int p=0;int b=0;
    Person[] woman=new Person[4];
    Person[] man=new Person[4];
    int k=0;int s=0;int r=0;
    boolean sign=true;
    boolean sign1=true;
    boolean sign2=true;
 
   /**内部类*/
 public class Person{
 
  private String name;
     private boolean sex;
  private Person spouse;
 
     public Person(String name){
      this.name=name;
      this.sex=false;
      this.spouse=null;
     }
  public Person( ){
   name=null;
   sex=false;
   spouse=null;
  }
    
  public Person(String name,boolean sex,Person spouse){
   this.name=name;
   this.sex=sex;
   this.spouse=spouse;
   
  }
  
  public void setName(String name){
   this.name=name;
  }
  
  public String getName( ){
   return name;
  }
 
  public void setSex(boolean sex){
   this.sex=sex;
  }
  public boolean getSex( ){
   return sex;
  }
  public void setSpouse (Person person1){
   this.spouse=person1;
  }
     public Person getSpouse( ){
      return spouse;
     }
 }
 
 /** 方法*/
 public void isCouple(Person person1,Person person2 ){
  if(person1.getSpouse().getName()==person2.getName()) {
     person1.setSex(!person2.getSex());
     person2.setSex(!person1.getSex());
     System.out.println(person1.getName()+"'s sex and "+person2.getName()+"'s sex is not equal" );
  } 
 }
 
 
 public void marriage(Person person1,boolean person1Marriage ,Person person2,boolean person2giveGiftPerson1 ){
  if( person1Marriage&&person2giveGiftPerson1)
  if(!person1.getName().equals(person2.getName()))
      System.out.println(person1.getName()+ " and "+person2.getName()+" are not couple");
         sortBySpouse1[x++]=person1;sortBySpouse2[y++]=person2;
 }       
 
 
 
 public void oneVolleyballGroup(boolean oneGroup,Person person1,Person person2 ){
  if(oneGroup&&person1.getSex()==person2.getSex())
     System.out.println(person1.getName()+" and "+person2.getName()+" is the same sex");
     sortByEqualSex1[n++]=person1;sortByEqualSex1[n++]=person2;b=n;
 } 
 
 
 
 public void liveCollectivityDorm(boolean liveTogether,Person person1,Person person2,Person person3 ){
  if(liveTogether&&!person1.getName().equals(person2.getName( ))&&!person1.getName().equals(person3.getName())&&!person2.getName().equals(person3.getName())){
     System.out.println(person1.getName()+"'s sex and "+person2.getName()+"'s sex is equal "+person3.getName()+"'s sex is equal");
     sortByEqualSex2[p++]=person1;sortByEqualSex2[p++]=person2;sortByEqualSex2[p++]=person3;
    }  
 }  
 
 
 public void isbrotherInLaw(Person person1,Person person2,boolean p1SIsBrotherInLawP2S ){
  if(!person1.getName().equals(person2.getName())&& p1SIsBrotherInLawP2S){
   person1.setSex(false);
   System.out.println(person1.getName()+" is a woman");
   woman[k++]=person1;r=k;
  } 
  System.out.println(person1.getName()+"'s sex is woman and "+person1.getName()+"'s spouse is man");
 } 
 
 
 public void assistInFighting (boolean cCoupleQuarrel,Person cCouple,Person person1,Person person2,Person person3){
  if (cCoupleQuarrel )
    if(!cCouple.getName().equals(person1.getName( ))&&!cCouple.getName().equals(person2.getName( ))&&!cCouple.getName().equals(person3.getName( ))
       &&!person1.getName().equals(person2.getName( ))&&!person1.getName().equals(person3.getName( ))&&!person2.getName().equals(person3.getName( ))){
  
   
       System.out.println(cCouple.getName()+"'s spouse is not "+person1.getName()+" and "+cCouple.getName()+"'s spouse is not "+person1.getName()+" 's spouse");
       System.out.println(cCouple.getName()+"'s spouse is not "+person2.getName()+" and "+cCouple.getName()+"'s spouse is not "+person2.getName()+" 's spouse");
       System.out.println(cCouple.getName()+"'s spouse is not "+person3.getName()+" and "+cCouple.getName()+"'s spouse is not "+person3.getName()+" 's spouse");
   }
      sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person1;
   sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person2;
   sortBySpouse1[x++]=cCouple;sortBySpouse2[y++]=person3; 
 } 
 
 
  public void guessCoupleByRules( ){
  /**事实数据*/
    Person[] person=new Person[8];
       person[0]=new Person("wang",false,person[0]);
       person[1]=new Person("chen",false,person[1]);
       person[2]=new Person("zhou",false,person[2]);
       person[3]=new Person("qian",false,person[3]);
       person[4]=new Person("wu",false,person[4]);
       person[5]=new Person("sun",false,person[5]);
       person[6]=new Person("li",false,person[6]);
       person[7]=new Person("xu",false,person[7]);
      /** 调用方法*/
       marriage(person[0],true ,person[2],true );
       oneVolleyballGroup(true,person[2],person[3] );
       isbrotherInLaw(person[6],person[1],true );
       assistInFighting (true,person[1],person[7],person[2],person[4]);
       liveCollectivityDorm(true,person[6],person[7],person[2]);
       /**计算*/
       for(int v=0;v<b;v++)
       for(int t=0;t<p;t++){
        if(sortByEqualSex1[v].getName().equals(sortByEqualSex2[t].getName())){
         for(int z=0;z<p;z++){
          for(int j=0;j<b;j++){
           if(!sortByEqualSex2[z].getName().equals(sortByEqualSex1[j].getName())) ;
           else{sortByEqualSex1[j]=sortByEqualSex2[z];sign2=false;}
           }
          if(sign2) sortByEqualSex1[n++]=sortByEqualSex2[z];
          sign2=true;
         }
          }
         }
       for(int m=0;m<r;m++)
       for(int q=0;q<n;q++){
        if(woman[m].getName().equals(sortByEqualSex1[q].getName())) {
            for(int l=0;l<n;l++){
                for(int j=0;j<r;j++){
                    if(!sortByEqualSex1[l].getName().equals(woman[j].getName())) ;
                    else { woman[j]=sortByEqualSex1[l];sign=false;}
                    }  
             if(sign) woman[k++]=sortByEqualSex1[l];
             sign=true;
          }
         }
         }
         r=k;
         if(r==4) {
          System.out.println("These person's sex is partitioning");
          System.out.println("The woman is");
          for(int i=0;i<r;i++)
          System.out.println(woman[i].getName()+"   ");
          for(int j=0;j<8;j++){
              int w=0;
          for(int i=0;i<r;i++){
            if(!person[j].getName().equals(woman[i].getName())) w++;
          } 
           if(w==r) man[s++]=person[j];
          }
         
          System.out.println("The man is");
          for(int i=0;i<s;i++)
          System.out.println(man[i].getName()+"   ");
        } 
      }  
         
     
     public static void main( String[] args){
         GuessCouple1 guessCouple=new GuessCouple1( );
         guessCouple.guessCoupleByRules();
  }
}    
      
      
           
 
 
 
 
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22218次
    • 积分:482
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:2篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论