[code="java"]A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束:
以前有做过
[code="java"]char[] c = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
int[] n = new int[c.length];
for (int i=0, t=i; i<(int)Math.pow(2, c.length); i++, t=i) {
for (int j=0; j n[n.length-j-1] = t%2;
t >>= 1;
}
boolean check = true;
check &= (n[0]==1 ? n[1]==1 : true); //1
check &= (n[2]==0 ? n[3]==0 : true); //2
check &= (n[0]+n[2] <= 1); //3
check &= (n[1]+n[3] == 1); //4
check &= (n[3]+n[4]+n[5]+n[6]+n[7] >= 2); //5
check &= (n[2]==n[6]); //6
check &= (n[2]+n[4]+n[6]+n[8] <= 2); //7
check &= (n[4]==1 ? n[5]==1 && n[6]==1 : true); //8
check &= (n[5]==1 ? n[6]==0 && n[7]==0 : true); //9
check &= (n[8]==0 && n[9]==0 ? n[7]==1 : true); //10
if (check) {
for (int j=0; j<n.length; j++) {
if (n[j] == 1) {
System.out.printf("%c ", c[j]);
}
}
System.out.println();
}
}[/code]
如下是一个类似的问题
10重嵌套循环/可以改成递归
public class HuiYi {
// 1:参加会议:有人邀请A,B,C,D,E,F6个人参加一项会议,这6个人有些奇怪,因为他们有很多要求,已知:
// 1.A,B两人至少有1人参加会议。
// 2.A,E,F3人中有2人参加会议。
// 3.B和C两人一致决定,要么两人都去,要么两人都不去。
// 4.A,D两人中只1人参加会议。
// 5.C,D两人中也只要1人参加会议。
// 6.如果D不去,那么E也决定不去。
// 那么最后究竟有哪几个人参加了会议呢?
public static void main(String[] args) {
for(int a1=1;a1<=2;a1++){
for(int a2=1;a2<=2;a2++){
for(int a3=1;a3<=2;a3++){
for(int a4=1;a4<=2;a4++){
for(int a5=1;a5<=2;a5++){
for(int a6=1;a6<=2;a6++){
if(sum(a1,a2)>=1){
//
}else{
continue;
}
if(sum(a1,a5,a6)==2){
//
}else{
continue;
}
if(sum(a2,a3)==0 || sum(a2,a3)==2){
//
}else{
continue;
}
if(sum(a1,a4)==1){
//
}else{
continue;
}
if(sum(a3,a4)==1){
//
}else{
continue;
}
if(a4==2){
if(a5==2){
//
}else{
continue;
}
}
System.out.println("a1="+a1+",a2="+a2+",a3="+a3+",a4="+a4+",a5="+a5+",a6="+a6);
}
}
}
}
}
}
}
private static int sum(int ... ps){
int sum = 0 ;
for(int a : ps){
if(a==1){
sum ++;
}
}
return sum;
}
}