A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
  人参加了竞赛:   (1)A参加时,B也参加;   (2)B和C只有一个人参加;   (3)C和D或者都参加,或者都不参加;   (4)D和E中至少有一个人参加;   (5)如果E参加,那么A和D也都参加。

解决方案 »

  1.   

    public class d { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    f();
    }



    public static void f()
    {
    for(int i=0;i<=1;i++)
    {
    for(int j=0;j<=1;j++)
    {
    for(int k=0;k<=1;k++)
    {
    for(int l=0;l<=1;l++)
    {
    for(int m=0;m<=1;m++)
    {
    //判断E是否参加

    //E参加
    if(m==1&&i==1&&l==1&&j==1&&k==0&&l==0)
    {

    }
                            if(m==0&&l==1&&k==1&&j==0&&i==0)
                            {
                             System.out.println(i);
                             System.out.println(j);
                             System.out.println(k);
                             System.out.println(l);
                             System.out.println(m);
                            }

    }
    }
    }
    }
    }
    }






    }
      

  2.   

      (1)A参加时,B也参加; 
      (2)B和C只有一个人参加; 
      (3)C和D或者都参加,或者都不参加; 
      (4)D和E中至少有一个人参加; 
      (5)如果E参加,那么A和D也都参加。 可以从4入手。
    假设E参加了。那么A和D都参加,由1推出B也参加,之后由3推出C也会参加,再由2得知B不参加,这时结论和1矛盾。
    那么D一定是参加了。从3得知C也参加,再由2得知B没参加。假设A参加了B也会参加。因此A是不能参加的。
      

  3.   


    package com.john.game;public class TestLogic {
    Student stuA, stuB, stuC, stuD, stuE;
    public static void main(String[] args) {
    TestLogic tl = new TestLogic();
    tl.printJoinList(); }
    //重置所有学生状态
    public void  RestState(){
    stuA.setJoin(false);
    stuB.setJoin(false);
    stuC.setJoin(false);
    stuD.setJoin(false);
    stuE.setJoin(false);
    }
    TestLogic(){
     stuA = new Student("A");
     stuB = new Student("B");
     stuC = new Student("C");
     stuD = new Student("D");
     stuE = new Student("E");

    }
    //输出所有学生参赛情况
    public void printJoinList(){
    testEnable(stuA);
    testEnable(stuB);
    testEnable(stuC);
    testEnable(stuD);
    testEnable(stuE);
    }
    public void  testEnable(Student test){
    boolean result = false;
    RestState();
    test.setJoin(true);
    try{
    Rule1();
    Rule2();
    Rule3();
    Rule4();
    if (stuE.getJoin()){
    result = Rule5();
    }else{
    result = true; //到此没有任何违法规则的情况,可认为是参加的
    }
    if (result){
    System.out.println(test + " Join the game ");
            System.out.print(stuA + " join: " + stuA.getJoin() + " ");          System.out.print(stuB + " join: " + stuB.getJoin() + " ");
    System.out.print(stuC + " join: " + stuC.getJoin() + " ");
    System.out.print(stuD + " join: " + stuD.getJoin() + " ");
    System.out.print(stuE + " join: " + stuE.getJoin() + " ");

    }else{
    System.out.print(test + " Cant Join the game");

    }
    System.out.println("\n");
    }
    finally{
    test.setJoin(false);
    }
    }
    public void Rule1(){
    if (stuA.getJoin()){
    stuB.setJoin(true);
    }
    }

    public void Rule2(){
    stuC.setJoin(!stuB.getJoin());
    }
    public void Rule3(){
    stuD.setJoin(stuC.getJoin());
    }
    public void Rule4(){
    if (!stuD.getJoin()){
    stuE.setJoin(true);
    }
    }
    public boolean Rule5(){
    if (!stuA.getJoin()){
    stuA.setJoin(true);
    Rule1();
    Rule2();
    Rule3();
    Rule4();
    }else if (!stuD.getJoin()){
    stuD.setJoin(true);
    Rule1();
    Rule2();
    Rule3();
    Rule4();
    }

    if ((stuA.getJoin()) && (stuD.getJoin())){
    return true;

    }else{
    return false;
    }
    }
    }
    class Student {
    private boolean join = false;
    private  String name;
    public Student(String strName){
    name = strName;
    }
    public void setJoin(boolean bJoin){
    join = bJoin;
    }
    public boolean getJoin(){
    return join;
    }
    public String toString(){
    return "Student" + name;
    }
    }
      

  4.   

    public class MyTest { public static void main(String[] args) {
    // TODO Auto-generated method stub
    boolean a, b, c, d, e;
    a=b=c=d=e=true;
    int result = 5;
    for(int i=1; i<1<<5; i++) {
    result = 5;
    a = (i & 1) > 0;
    b = (i & 1<<1) > 0;
    c = (i & 1<<2) > 0;
    d = (i & 1<<3) > 0;
    e = (i & 1<<4) > 0;
    if(!a || b)
    result--;
    if(!b || !c)
    result--;
    if(c==d)
    result--;
    if(d || e)
    result--;
    if(!e || a &&d )
    result--;
    if(result == 0)
    break;
    }
    if(a)
    System.out.println("a参加了");
    if(b)
    System.out.println("b参加了");
    if(c)
    System.out.println("c参加了");
    if(d)
    System.out.println("d参加了");
    if(e)
    System.out.println("e参加了");
    }
    }
      

  5.   


    import static java.lang.System.out;public class Contest
    {
    public  static boolean isValid(int i)
    {
    if((i&16)==16 && (i&8)!=8)
    {
    return false;
    }
    if((i&8)==8 && (i&4)==4 || (i&8)==0 && (i&4)==0)
    {
    return false;
    }
    if((i&4)==4 && (i&2)==0 || (i&4)==0 && (i&2)==2)
    {
    return false;
    }
    if((i&2)==0 && (i&1)==0)
    {
    return false;
    }
    if((i&1)==1 &&((i&16)==0 || (i&2)==0))
    {
    return false;
    }
    return true;
    }
    public static void main(String[] args)
    {
    for(int i=0; i<32;i++)
    {
    if(isValid(i))
    {
    out.println("answer is:");
    if((i&16)==16)
    {
    out.println("A");
    }
    if((i&8)==8)
    {
    out.println("B");
    }
    if((i&4)==4)
    {
    out.println("C");
    }
    if((i&2)==2)
    {
    out.println("D");
    }
    if((i&1)==1)
    {
    out.println("E");
    }
    }
    }
    }
    }