java实现逻辑推断

A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束:

  1. 如果A参加,B也参加;
  2. 如果C不参加,D也不参加;
  3. A和C中只能有一个人参加;
  4. B和D中有且仅有一个人参加;
  5. D、E、F、G、H 中至少有2人参加;
  6. C和G或者都参加,或者都不参加;
  7. C、E、G、I中至多只能2人参加
  8. 如果E参加,那么F和G也都参加。
  9. 如果F参加,G、H就不能参加
  10. 如果I、J都不参加,H必须参加

请编程根据这些条件判断这10名同学中参赛者名单。如果有多种可能,则输出所有的可能情况。每种情况占一行。参赛同学按字母升序排列,用空格分隔。

比如:
C D G J
就是一种可能的情况。

多种情况的前后顺序不重要

package com.liu.ex4;

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static ArrayList<Integer> list = new ArrayList<Integer>();
    
    public boolean judge1() {
        boolean judge = true;
        if(list.contains(0)) {
            if(!list.contains(1))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge2() {
        boolean judge = true;
        if(!list.contains(2)) {
            if(list.contains(3))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge3() {
        boolean judge = true;
        if(list.contains(0)) {
            if(list.contains(2))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge4() {
        boolean judge = false;
        if(list.contains(1) && !list.contains(3))
            judge = true;
        else if(!list.contains(1) && list.contains(3))
            judge = true;
        return judge;
    }
    
    public boolean judge5() {
        boolean judge = false;
        int count = 0;
        for(int i = 3;i <= 7;i++) {
            if(list.contains(i))
                count++;
        }
        if(count >= 2)
            judge = true;
        return judge;
    }
    
    public boolean judge6() {
        boolean judge = false;
        if(list.contains(2) && list.contains(6))
            judge = true;
        else if(!list.contains(2) && !list.contains(6))
            judge = true;
        return judge;
    }
    
    public boolean judge7() {
        boolean judge = false;
        int count = 0;
        if(list.contains(2))
            count++;
        if(list.contains(4))
            count++;
        if(list.contains(6))
            count++;
        if(list.contains(8))
            count++;
        if(count <= 2)
            judge = true;
        return judge;
    }
    
    public boolean judge8() {
        boolean judge = true;
        if(list.contains(4)) {
            if(list.contains(5) == false || list.contains(6) == false)
                judge = false;
        }
        return judge;
    }
    
    public boolean judge9() {
        boolean judge = true;
        if(list.contains(5)) {
            if(list.contains(6) || list.contains(7))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge10() {
        boolean judge = true;
        if(!list.contains(8) && !list.contains(9)) {
            if(!list.contains(7))
                judge = false;
        }
        return judge;
    }
    
    public boolean check() {
        if(judge1() && judge2() && judge3() &&  judge4() && judge5()) {
            if(judge6() && judge7() && judge8() && judge9() && judge10())
                return true;
        }
        return false;
    }
    
    public void dfs(int step) {
        while(step < 10) {
            list.add(step);
            if(check()) {
                ArrayList<Integer> tempList = new ArrayList<Integer>();
                for(int i = 0;i < list.size();i++)
                    tempList.add(list.get(i));
                Collections.sort(tempList);
                for(int i = 0;i < tempList.size();i++) {
                    char temp = (char) ('A' + tempList.get(i));
                    System.out.print(temp+" ");
                }
                System.out.println();
            }
            step++;
            dfs(step);
            list.remove(list.size() - 1);
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.dfs(0);
    }
}
posted @ 2019-07-25 19:21  南墙1  阅读(20)  评论(0编辑  收藏  举报