public class GuessRank {
public static void main(String[] args) {
for(int a = 1; a<=5; a++){
for(int b = 1; b<=5; b++){
for(int c = 1; c<=5; c++){
for(int d = 1; d<=5; d++){
for(int e = 1; e<=5; e++){
if(toMeetAllTheConditions(a,b,c,d,e)) {
System.out.println("a:"+a+" b:"+b+" c:"+c+" d:"+d+" e:"+e);
}
}
}
}
}
}
}
private static boolean toMeetAllTheConditions(int a, int b, int c, int d, int e) {
if( (a==b)||(a==c)||(a==d)||(a==e)||(b==c)||(b==d)||(b==e)||(c==d)||(c==e)||(d==e) ) return false; if( (e==2)||(e==3) ) return false; if( ((a==1)||(a==2)) && (e!=1) ) return false;
if( ((a!=1)&&(a!=2)) && (e==1) ) return false; if( ((b==1)||(b==2)) && (b!=2) ) return false;
if( ((b!=1)&&(b!=2)) && (b==2) ) {/*DoNothing*/} if( ((c==1)||(c==2)) && (a!=5) ) return false;
if( ((c!=1)&&(c!=2)) && (a==5) ) return false; if( ((d==1)||(d==2)) && (c==1) ) return false;
if( ((d!=1)&&(d!=2)) && (c!=1) ) return false; if( ((e==1)||(e==2)) && (d!=1) ) return false;
if( ((e!=1)&&(e!=2)) && (d==1) ) return false; return true;
}
}

解决方案 »

  1.   

    public class GuessRank {
        public static void main(String[] args) {
            for(int a = 1; a<=5; a++){
                for(int b = 1; b<=5; b++){
                    for(int c = 1; c<=5; c++){
                        for(int d = 1; d<=5; d++){
                            for(int e = 1; e<=5; e++){
                                if(toMeetAllTheConditions(a,b,c,d,e)) {
                                    System.out.println("a:"+a+" b:"+b+" c:"+c+" d:"+d+" e:"+e);
                                }
                            }
                        }
                    }
                }
            }
        }
        private static boolean toMeetAllTheConditions(int a, int b, int c, int d, int e) {
            if( (a==b)||(a==c)||(a==d)||(a==e)||(b==c)||(b==d)||(b==e)||(c==d)||(c==e)||(d==e) ) return false;        if( (e==2)||(e==3) ) return false;        if( ((a==1)||(a==2)) && (e!=1) ) return false;
            if( ((a!=1)&&(a!=2)) && (e==1) ) return false;        if( ((b==1)||(b==2)) && (b!=2) ) return false;
            if( ((b!=1)&&(b!=2)) && (b==2) ) {/*DoNothing*/}        if( ((c==1)||(c==2)) && (a!=5) ) return false;
            if( ((c!=1)&&(c!=2)) && (a==5) ) return false;        if( ((d==1)||(d==2)) && (c==1) ) return false;
            if( ((d!=1)&&(d!=2)) && (c!=1) ) return false;        if( ((e==1)||(e==2)) && (d!=1) ) return false;
            if( ((e!=1)&&(e!=2)) && (d==1) ) return false;        return true;
        }
    }
      

  2.   

    执行结果:a:5 b:2 c:1 d:3 e:4
      

  3.   


    public class t {
    public static void main(String[] args) {
    // 6、有A、B、C、D和E共5所学校。在一次检查评比中,已知E肯定不是第2或第3名,
    // 他们互相进行推测,A校有人说,E校肯定是第1名;B校有人说,我校可能是第2名;
    // C校有人说,A校最差;D校有人说,C校不是最好;E校有人说,D校会获得第一名。
    // 结果只有第一名和第二名的学校的人猜对了。编程指出这5所学校的名词。
    // 条件:E肯定不是第2或第3名(E!=2&&E!=3) 第一名和第二名的学校的人猜对了 其它的都错了
    // A校有人说,E校肯定是第1名;(A不能是第一名或第二名)
    // B校有人说,我校可能是第2名 (B校不能是第一名)
    // C校有人说,A校最差;
    // D校有人说,C校不是最好
    // E校有人说,D校会获得第一名

    String[] str = { "A", "B", "C", "D", "E" };//五个人
    System.out.println("1 2 3 4 5");
    for (int i = 0; i < 5; i++) {// 第一名
    for (int j = 0; j < 5; j++) {// 第二名
    for (int k = 0; k < 5; k++) {// 第三名
    for (int l = 0; l < 5; l++) {// 第四名
    for (int m = 0; m < 5; m++) {// 第五名
    boolean dyi = false;//第一名
    boolean der = false;//第二名
    boolean dsan = true;//第三名
    boolean dsi = true;//第四名
    boolean dwu = true;//第五名
    if (j != i && k != i && k != j && l != i && l != j
    && l != k && m != i && m != j && m != k
    && m != l) {// 消除重复
    if (4 != j && 4 != k) {//E不能为第二名也不能为第三名
    if (i == 0) {// A为第一名时
    if (i == 4) {// E为第一名
    dyi = true;
    }
    }
    if (j == 0) {// A为第二名时
    if (i == 4) {// E为第一名
    der = true;
    }
    }
    if (i == 1) {// B为第一名时
    if (j == 1) {// B为第二名
    dyi = true;
    }
    }
    if (j == 1) {// B为第二名时
    if (j == 1) {// B为第二名
    der = true;
    }
    }
    if (i == 2) {// C为第一名时
    if (m == 0) {// A为第五名
    dyi = true;
    }
    }
    if (j == 2) {// C为第二名时
    if (m == 0) {// A为第五名
    der = true;
    }
    }
    if (i == 3) {// D为第一名时
    if (i != 2) {// C不等于第一名
    dyi = true;
    }
    }
    if (j == 3) {// D为第二名时
    if (i != 2) {// C不等于第一名
    der = true;
    }
    }
    if (i == 4) {// E为第一名时
    if (i == 3) {// D为第一名
    dyi = true;
    }
    }
    if (j == 4) {// E为第二名时
    if (i == 3) {// D为第一名
    der = true;
    }
    }
    // 三四五名均为false
    if (k == 0) {// 第三名为A时
    if (i != 4) {
    dsan = false;
    }
    }
    if (k == 1) {// 第三名为B时
    if (j != 1) {
    dsan = false;
    }
    }
    if (k == 2) {// 第三名为C时
    if (m != 0) {// A不为最差
    dsan = false;
    }
    }
    if (k == 3) {// 第三名为D时
    if (i == 2) {// C校是最好
    dsan = false;
    } }
    if (l == 0) {// 第四名为A时
    if (i != 4) {
    dsi = false;
    }
    }
    if (l == 1) {// 第四名为B时
    if (j != 1) {
    dsi = false;
    }
    }
    if (l == 2) {// 第四名为C时
    if (m != 0) {// A不为最差
    dsi = false;
    }
    }
    if (l == 3) {// 第四名为D时
    if (i == 2) {// C校是最好
    dsi = false;
    } }
    if (l == 4) {// 第四名为E时
    // D校不等于第一名
    if (i != 3) {
    dsi = false;
    }
    }
    if (m == 0) {// 第五名为A时
    if (i != 4) {
    dwu = false;
    }
    }
    if (m == 1) {// 第五名为B时
    if (j != 1) {
    dwu = false;
    }
    }
    if (m == 2) {// 第五名为C时
    if (m != 0) {// A不为最差
    dwu = false;
    }
    }
    if (m == 3) {// 第五名为D时
    if (i == 2) {// C校是最好
    dwu = false;
    } }
    if (m == 4) {// 第五名为E时
    // D校不等于第一名
    if (i != 3) {
    dwu = false;
    }
    }
    if (dyi == true && der == true
    && dsan == false && dsi == false
    && dwu == false) {
    System.out.println(str[i] + " "
    + str[j] + " " + str[k] + " "
    + str[l] + " " + str[m]);
    }
    } } } }
    } } }

    }}