关于谁来参加会议这个题目的卫条件

 

package Meeting;//卫条件练习
/*
* 有人邀请A,B,C,D,E,F 6个人参加一项会议,这6个人有些奇怪,因为他们有很多要求,已知:
1)A,B两人至少有1人参加会议;
2)A,E,F 3人中有2人参加会议;
3)B和C两人一致决定,要么两人都去,要么两人都不去;
4)A,D两人中只1人参加会议;
5)C,D两人中也只要1人参加会议;
6)如果D不去,那么E也决定不去。
那么最后究竟有哪几个人参加了会议呢?
*/
public class attendeeOfMeeting {

public static void main(String[] args) {//0代表不去,1代表去
for(int a=0;a<2;a++){
for(int b=0;b<2;b++){
for(int c=0;c<2;c++){
for(int d=0;d<2;d++){
for(int e=0;e<2;e++){
for(int f=0;f<2;f++){
if(condition(a,b,c,d,e,f)){
System.out.println("a="+a+",b="+b+",c="+c+",d="+d+",e="+e+",f="+f);
}
}
}
}
}
}
}
}

private static int addGo(int...args){//※可变参数※
int sum=0;
for(int i:args){
if(i==1){//统计有几人去
sum ++;
}
}
return sum;
}
//0--不去,1--去
private static boolean condition(int a, int b, int c, int d, int e, int f) {
if( !(addGo(a,b)>=1) ){//卫条件1)A,B两人至少有1人参加会议;
return false;
}
if( !(addGo(a,e,f)==2) ){//卫条件2)A,E,F 3人中有2人参加会议;
return false;
}
if( addGo(b,c)==1 ){//卫条件3)B和C两人一致决定,要么两人都去,要么两人都不去;
return false;
}
if( !(addGo(a,d)==1) ){//卫条件4)A,D两人中只1人参加会议;
return false;
}
if( !(addGo(c,b)==1) ){//卫条件5)C,D两人中也只要1人参加会议;
return false;
}
if( d==0 && e==1 ){//卫条件6)如果D不去,那么E也决定不去。
return false;
}
return true;
}
}

posted @ 2016-06-20 12:21  折腾青春  阅读(200)  评论(0编辑  收藏  举报