質問の公務員試験の場合の数の問題をJavaで解いてみた。

 質問の公務員試験の場合の数の問題Javaで解いてみました。(^_^;
 特に、2問目は、数が少ないので、樹形図で解くのが一番いいのかも。(^_^;

●Outcomes2.java

/*
 * Outcomes2.java
 */

class Outcomes2 {
    
    static boolean next_perm(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 void main(String[] args) {
        char[] p ="ABCD".toCharArray();
        int count;
        long tm;
        
        System.out.println("問1)");
        tm=System.nanoTime();       // Timer Start
        count=0;
        do{
            String s = new String(p);   // 隣接したらカウントして表示
            if(Math.abs(s.indexOf('A')-s.indexOf('B'))==1)
                System.out.printf("[%2d] : %s\n", ++count, s);
        }while(next_perm(p,4,4));
        
        tm=System.nanoTime()-tm;        // Timer Stop
        System.out.printf("Runtime : %.3f [sec]\n",(double)tm/1e9);
        
        System.out.println("問2)");
        tm=System.nanoTime();       // Timer Start
        count=0;
        for(int a=1; a<=6; a++)
            for(int b=1; b<=6; b++)
                if((10*a+b)%8==0)
                    System.out.printf("[%2d] : %s\n", ++count, 10*a+b);
        
        tm=System.nanoTime()-tm;        // Timer Stop
        System.out.printf("Runtime : %.3f [sec]\n",(double)tm/1e9);
    }
}

●実行結果

問1)
[ 1] : ABCD
[ 2] : ABDC
[ 3] : BACD
[ 4] : BADC
[ 5] : CABD
[ 6] : CBAD
[ 7] : CDAB
[ 8] : CDBA
[ 9] : DABC
[10] : DBAC
[11] : DCAB
[12] : DCBA
Runtime : 0.025 [sec]
問2)
[ 1] : 16
[ 2] : 24
[ 3] : 32
[ 4] : 56
[ 5] : 64
Runtime : 0.002 [sec]

 ちなみに、Pythonなら、こんな感じで出来ます。やっぱり楽だね。(^_^;
●Outcomes2.py

# Outcomes2.py
import itertools
from time import time
print "Q.1)"
tm=time()  # Timer Start
c=0
for p in itertools.permutations(['A','B','C','D']):
  s=p[0]+p[1]+p[2]+p[3]
  if abs(s.index('A')-s.index('B'))==1:
    c+=1
    print "[%2d] : %s"%(c,s)
tm=time()-tm  # Timer Stop
print "Runtime : %.3f [sec]"%tm

print "Q.2)"
tm=time()  # Timer Start
c=0
for a in range(1,6+1):
  for b in range(1,6+1):
    if (10*a+b)%8==0:
      c+=1
      print "[%2d] : %2d"%(c,10*a+b)
tm=time()-tm  # Timer Stop
print "Runtime : %.3f [sec]"%tm

●実行結果

Q.1)
[ 1] : ABCD
[ 2] : ABDC
[ 3] : BACD
[ 4] : BADC
[ 5] : CABD
[ 6] : CBAD
[ 7] : CDAB
[ 8] : CDBA
[ 9] : DABC
[10] : DBAC
[11] : DCAB
[12] : DCBA
Runtime : 0.002 [sec]
Q.2)
[ 1] : 16
[ 2] : 24
[ 3] : 32
[ 4] : 56
[ 5] : 64
Runtime : 0.001 [sec]

Python でアバウトな時間計測 | すぐに忘れる脳みそのためのメモ
http://jutememo.blogspot.jp/2008/09/python_22.html

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

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

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

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

みんなのPython 第3版

みんなのPython 第3版