判断推理の過去問にチャレンジ!の問題をJavaで解いてみた。

 判断推理(地方公務員[大卒])の過去問にチャレンジ!の問題Javaで解いてみました。

 +---+---+---+---+---+---+---+
 | # | 1 | 2 | 3 | 4 | 5 | 6 |
 +---+---+---+---+---+---+---+
 | R |   |   |   |   |   |   |
 +---+---+---+---+---+---+---+
 | A |   |   |   |   |   |   | 5
 +---+---+---+---+---+---+---+
 | B |   |   |   |   |   |   | 4
 +---+---+---+---+---+---+---+
 | C |   |   |   |   |   |   | 2
 +---+---+---+---+---+---+---+

● QuestionTable1.java

/* 
 * QuestionTable1.java
 */

class QuestionTable1 {
    // 順列生成
    static boolean nextPerm(char[] p, int n, int r) {
        int i, j;
        char t;
        
        if(r <= 0 || n < r) return(false);
        for(i = r + 1; i <= n-1; i++)
            for(j = i; j >= r + 1 && p[j-1] < p[j]; j--){
                t = p[j]; p[j] = p[j-1]; p[j-1] = t;    // swap(p,j,j-1);
            }
        for(i = n - 1; i > 0 && p[i-1] >= p[i]; i--);
        if(i==0) return(false);
        for(j = n - 1; j > i && p[i-1] >= p[j]; j--);
        t = p[j]; p[j] = p[i-1]; p[i-1] = t;            // swap(p,j,i-1);
        for(j = n - 1; i < j; i++, j--){
            t = p[i]; p[i] = p[j]; p[j] = t;            // swap(p,i,j);
        }
        return(true);
    }
    
    public static int countMatches(char[] a, char[] b) {
        int n = a.length;
        int count = 0;
        for(int i=0; i< n; i++)
            if(a[i]==b[i]) count++;
        return(count);
    }
    
    public static int countMatches(String s, char c) {
        int n = s.length();
        int count = 0;
        for(int i=0; i< n; i++)
            if(s.charAt(i)==c) count++;
        return(count);
    }
    
    public static void main(String[] args) {
        final String A = "アアアアアイ";
        final String B = "アアアアイイ";
        final String C = "アアイイイイ";
        
        char[] a = A.toCharArray(); // Aの解答
        char[] b = B.toCharArray(); // Bの解答
        char[] c = C.toCharArray(); // Cの解答
        char[] r = new char[6];     // 正解
        String[] sTbl = new String[6];
        boolean[] ch = {true,true,true,true,true};  // 選択肢
        int iCount = 0;
        
        long tm=System.nanoTime();      // Timer Start
        a = A.toCharArray();
        do{    //--- a loop ---//
            b = B.toCharArray();
            do{    //--- b loop ---//
                c = C.toCharArray();
                do{    //--- c loop ---//
                    for(int j=0; j< 6; j++){    //--- r loop ---//
                        for(int i=0; i< 6; i++) r[i]=a[i];
                        r[j]=((a[j]=='ア') ? 'イ' : 'ア');  // 条件Aより
                        if(countMatches(r,b)!=4) continue;  // 条件B
                        if(countMatches(r,c)!=2) continue;  // 条件C
                        
                        // 問題番号ごとに正誤表を作成
                        for(int i=0; i< 6; i++) sTbl[i]="";
                        for(int i=0; i< 6; i++){
                            sTbl[i]+=((r[i]==a[i]) ? "○" : "×");
                            sTbl[i]+=((r[i]==b[i]) ? "○" : "×");
                            sTbl[i]+=((r[i]==c[i]) ? "○" : "×");
                        }
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(sTbl[i].equals("○○○")) iCount++;
                        if(iCount!=1) continue;         // 3人とも正解の条件
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(sTbl[i].equals("×××")) iCount++;
                        if(iCount!=1) continue;         // 3人とも不正解の条件
                        // チェックをくぐり抜けたものだけを表示
                        System.out.println("#:123456");
                        System.out.println("R:"+new String(r));
                        System.out.print("A:"+new String(a)+":");
                        for(int i=0; i< 6; i++) System.out.print(sTbl[i].charAt(0));
                        System.out.println();
                        System.out.print("B:"+new String(b)+":");
                        for(int i=0; i< 6; i++) System.out.print(sTbl[i].charAt(1));
                        System.out.println();
                        System.out.print("C:"+new String(c)+":");
                        for(int i=0; i< 6; i++) System.out.print(sTbl[i].charAt(2));
                        System.out.println();
                        System.out.println();
                        
                        //---  選択肢(1)のチェック ---//
                        if(countMatches(new String(r),'ア')==4) ch[0]&=true; else ch[0]&=false;
                        //---  選択肢(2)のチェック ---//
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(sTbl[i].equals("○○×")) iCount++;
                        if(iCount==3) ch[1]&=true; else ch[1]&=false;
                        //---  選択肢(3)のチェック ---//
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(sTbl[i].substring(1).equals("○○")) iCount++;
                        if(iCount==2) ch[2]&=true; else ch[2]&=false;
                        //---  選択肢(4)のチェック ---//
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(r[i]!=a[i] && a[i]=='ア') iCount++;
                        if(iCount==1) ch[3]&=true; else ch[3]&=false;
                        //---  選択肢(5)のチェック ---//
                        iCount = 0;
                        for(int i=0; i< 6; i++)
                            if(r[i]==c[i] && c[i]=='イ') iCount++;
                        if(iCount==2) ch[4]&=true; else ch[4]&=false;
                    }
                }while(nextPerm(c,6,6));
            }while(nextPerm(b,6,6));
        }while(nextPerm(a,6,6));
        
        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);
    }
}

●実行結果

…(省略)…

#:123456
R:イアアアイア
A:イアアアアア:○○○○×○
B:イイアアアア:○×○○×○
C:イアイイアイ:○○××××

#:123456
R:アアアアアア
A:イアアアアア:×○○○○○
B:イイアアアア:××○○○○
C:イアイイイア:×○×××○

#:123456
R:イアアアアイ
A:イアアアアア:○○○○○×
B:イイアアアア:○×○○○×
C:イアイイイア:○○××××

∴ 2
Runtime : 0.676 [sec]

公務員試験 新スーパー過去問ゼミ3 判断推理 改訂版

公務員試験 新スーパー過去問ゼミ3 判断推理 改訂版

  • 作者: 資格試験研究会
  • 出版社/メーカー: 実務教育出版
  • 発売日: 2012/10/24
  • メディア: 単行本(ソフトカバー)
  • 購入: 2人 クリック: 3回
  • この商品を含むブログを見る
判断推理がみるみるわかる! 解法の玉手箱[改訂版]

判断推理がみるみるわかる! 解法の玉手箱[改訂版]

上・中級公務員 標準判断推理―確かな解答力が身につく“基本書”

上・中級公務員 標準判断推理―確かな解答力が身につく“基本書”

公務員試験 判断推理必殺の解法パターン

公務員試験 判断推理必殺の解法パターン

畑中敦子の[判断推理・数的推理]苦手克服ノート (別冊受験ジャーナル)

畑中敦子の[判断推理・数的推理]苦手克服ノート (別冊受験ジャーナル)

  • 作者: 畑中敦子,受験ジャーナル編集部
  • 出版社/メーカー: 実務教育出版
  • 発売日: 2010/03/06
  • メディア: ?
  • 購入: 3人 クリック: 4回
  • この商品を含むブログを見る