センター試験の原因の確率の問題をJavaで解いてみた。

 センター試験の原因の確率の問題をJavaで解いてみました。(^_^;
 拙ブログのWRCondProb1.javaが雛形になりそうなのでJavaを使いました。(^_^;

 赤球4個、青球3個、白球5個、合計12個の球を袋からA,Bの順に戻さずに1個ずつ取り出す。
(1)[ア-エ] AとBが取り出した2個の球の中に赤球か青球が少なくとも1個含まれている確率
(2)[オ-キ] Aが赤球、Bが白球を取り出す確率
(2)[ク-コ] Aが取り出した球が赤球であったとき、Bが取り出した球が白球である条件付き確率
(3)[オ-キ] Aが赤球、Bが白球を取り出す確率
(3)[サ-ス] Aが青球、Bが白球を取り出す確率
(3)[セ-タ] Bが白球を取り出す確率
(3)[チ-テ] Bが取り出した球が白球であったとき、Aが取り出した球が白球であった条件付き確率

 確率の問題では見た目が同じものでも区別して考えるので、次のように球に番号を与えて、その番号を5で割った商でそれぞれの色を得ることにします。また、Aが取り出した球をp[0]、Bが取り出した球をp[1]として、順列を生成して条件に合う場合の数をカウントしてそれぞれの確率を求めました。

白球(0): 0, 1, 2, 3, 4,
赤球(1): 5, 6, 7, 8,
青球(2): 10,11,12

 ちなみに、(1)[ア-エ]、(2)[ク-コ]、(3)[チ-テ]は次のように言い換えることが出来ます。

(1)[ア-エ] AまたはBが白球を取り出さない確率
(2)[ク-コ] Aが赤球を取り出す場合のうちBが白球を取り出す確率
(3)[チ-テ] Bが白球を取り出す場合のうちAが白球を取り出す確率

 それから、(2),(3)[オ-キ]は2通りの方法で求めてみました。

● WRBCondProb1.java

/* 
 * WRBCondProb1.java
 */

public class WRBCondProb1 {
    // 順列生成
    static boolean nextPerm(int[] p, int n, int r) {
        int i, j;
        int 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);
    }
    // 最大公約数
    static int gcd(int a, int b){ return( b == 0 ? a : gcd(b, a % b) ); }
    
    static void DispAns(String msg, int num, int den){
        int g = (num*den!=0) ? gcd(num,den) : 1;
        System.out.printf("%s %3d / %3d = %2d / %2d\n", msg, num, den, num/g, den/g);
    }
    
    public static void main(String[] args) {
        final int N = 12, R = 2;
        //        |   白球  | 赤球  |  青球  |
        int[] p = {0,1,2,3,4,5,6,7,8,10,11,12};
        int[] cnt = {0,0,0,0,0,0,0,0};
        long tm = System.nanoTime();    // Timer Start
        do{
            cnt[0]++;                   // 全順列 12P2 
            if(p[0]/5!=0 || p[1]/5!=0) cnt[1]++;    // AまたはBが白球を取り出さない
            if(p[0]/5==1){              // Aが赤球
                cnt[2]++;
                if(p[1]/5==0) cnt[3]++; // Bが白球
            }
            if(p[1]/5==0){              // Bが白球
                cnt[4]++;
                if(p[0]/5==1) cnt[5]++; // Aが赤球
                if(p[0]/5==2) cnt[6]++; // Aが青球
                if(p[0]/5==0) cnt[7]++; // Aが白球
            }
        }while(nextPerm(p,N,R));
        DispAns("(1)[ア-エ]",cnt[1],cnt[0]);  // AまたはBが白球を取り出さない確率
        DispAns("(2)[オ-キ]",cnt[3],cnt[0]);  // Aが赤球、Bが白球を取り出す確率
        DispAns("(2)[ク-コ]",cnt[3],cnt[2]);  // Aが赤球を取り出す場合のうちBが白球を取り出す確率
        DispAns("(3)[オ-キ]",cnt[5],cnt[0]);  // Aが赤球、Bが白球を取り出す確率
        DispAns("(3)[サ-ス]",cnt[6],cnt[0]);  // Aが青球、Bが白球を取り出す確率
        DispAns("(3)[セ-タ]",cnt[4],cnt[0]);  // Bが白球を取り出す確率
        DispAns("(3)[チ-テ]",cnt[7],cnt[4]);  // Bが白球を取り出す場合のうちAが白球を取り出す確率
        tm = System.nanoTime()-tm;      // Timer Stop
        System.out.printf("Runtime : %.3f [sec]\n",(double)tm/1e9);
    }
}

●実行結果

(1)[ア-エ] 112 / 132 = 28 / 33
(2)[オ-キ]  20 / 132 =  5 / 33
(2)[ク-コ]  20 /  44 =  5 / 11
(3)[オ-キ]  20 / 132 =  5 / 33
(3)[サ-ス]  15 / 132 =  5 / 44
(3)[セ-タ]  55 / 132 =  5 / 12
(3)[チ-テ]  20 /  55 =  4 / 11
Runtime : 0.026 [sec]

※参考URL
大学入試センター試験|解答速報2016|予備校の東進
知恵袋の白玉と赤玉の原因の確率の問題をJavaで解いてみた。

明解Java 入門編

明解Java 入門編