判断推理の身長差の問題をJavaで解いてみた。

 知恵袋で見つけた判断推理の身長差の問題Javaで解いてみました。(^_^;

 AからEの身長を調べたところ、次のことが分かった。
(ア)AとBは5cm違い、
(イ)BとCは1cm、
(ウ)CとDは3cm、
(エ)DとEは2cm、
(オ)EとAは1cm違っている。
 以上のことから確実にいえるのはどれか。
①1番背が高いのは、AかEである。
②2番目に背が高いのは、BかCである。
③3番目に背が高いのは、Dである。
④4番目に背が高いのは、BかCである。
⑤1番背が低いのは、AかEである。

 ただし、条件に(ア)〜(オ)と名前をつけました。(^_^;

●HeightDiff1.java

/* 
 * HeightDiff1.java
 * http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12104028602
 */
 
import java.util.Arrays;

public class HeightDiff1 {
    // xがa[]の何番目にあるか調べるメソッド
    static int getPos(int[] a, int x) {
        for(int i=0; i< a.length; i++)
            if(a[i]==x) return(i);  // あるときは、0〜a.length-1を返す. 
        return(-1);                 // ないときは、-1を返す. 
    }
    
    // a[]の中で n 番目に大きな数値を取得するメソッド
    static int Large(int[] a, int n) {
        int m=a.length;
        int[] b = (int[])a.clone(); // a[]に影響を与えないようにするためにコピー
        Arrays.sort(b);
        if(n< 1 || m< n) n=1;       // n が範囲外の時は、n=1
        return(b[m-n]);
    }
    
    // a[]に先頭からA〜Eと名前をつけて、n 番目に大きいものの名前を取得するメソッド
    static final String  NAME ="ABCDE";
    static char getNameOfLarge(int[] a, int n) {
        return( NAME.charAt(getPos(a,Large(a,n))) );
    }
    
    // 選択肢のチェック
    static void checkChoices(int[] a, boolean[] c) {
        if(getNameOfLarge(a,1)=='A' || getNameOfLarge(a,1)=='E') c[0]&=true; else c[0]&=false;
        if(getNameOfLarge(a,2)=='B' || getNameOfLarge(a,2)=='C') c[1]&=true; else c[1]&=false;
        if(getNameOfLarge(a,3)=='D')                             c[2]&=true; else c[2]&=false;
        if(getNameOfLarge(a,4)=='B' || getNameOfLarge(a,4)=='C') c[3]&=true; else c[3]&=false;
        if(getNameOfLarge(a,5)=='A' || getNameOfLarge(a,5)=='E') c[4]&=true; else c[4]&=false;
    }
    
    public static void main(String[] args) {
        int a=0;    // aを0として、b〜eの相対値を調べる。
        boolean[] ch = {true,true,true,true,true};
        long tm=System.nanoTime();      // Timer Start
        
        for(int b : new int[] {a-5,a+5}) {              // 条件ア
            for(int c : new int[] {b-1,b+1}){           // 条件イ
                for(int d : new int[] {c-3,c+3}){       // 条件ウ
                    for(int e : new int[] {d-2,d+2}){   // 条件エ
                        if(Math.abs(a-e)!=1) continue;  // 条件オ
                        int[] n = new int[] {a,b,c,d,e};
                        // チェックを潜り抜けたものだけを表示
                        System.out.print(getNameOfLarge(n,1));
                        System.out.print(getNameOfLarge(n,2));
                        System.out.print(getNameOfLarge(n,3));
                        System.out.print(getNameOfLarge(n,4));
                        System.out.print(getNameOfLarge(n,5));
                        System.out.print(" : ");
                        System.out.println(Arrays.toString(n));
                        // 選択肢のチェック
                        checkChoices(n, ch);
                    }
                }
            }
        }
        System.out.print("∴");
        for(int i=0; i< 5; i++)
            if(ch[i]) System.out.println(" "+(i+1));
        tm=System.nanoTime()-tm;        // Timer Stop
        System.out.printf("Runtime : %.3f [sec]\n",(double)tm/1e9);
    }
}

●実行結果

AEDBC : [0, -5, -6, -3, -1]
EADCB : [0, -5, -4, -1, 1]
BCDAE : [0, 5, 4, 1, -1]
CBDEA : [0, 5, 6, 3, 1]
∴ 3
Runtime : 0.003 [sec]