質問のカードの確率の問題をJavaで解いてみました。(^_^;
組合せを生成して、P(a< b),P(a==b),P(a> b)を求めてみました。途中、b=2,4,6,...,20のときのa>b,a=b,a
/* * ProbOfCards3.java * */ class ProbOfCards3 { // 順列生成 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 boolean nextComb(int[] p, int n, int r) { int i; do{ if(!nextPerm(p,n,r)) return(false); for(i=0; i< r-1; i++) if(p[i]> p[i+1]) break; } while (i< r-1); return(true); } static int gcd(int a, int b){ return( b == 0 ? a : gcd(b, a % b) ); } public static void main(String[] args) { final int N = 10, R = 2; final int[] A = {1,2,3,4,5,6,7,8,9,10}; int total = 0, countA = 0, countB = 0, countE = 0, g = 1; int[] a = new int[N]; // 組合せ生成用 long tm=System.nanoTime(); // Timer Start System.out.println(" b:a>b,a=b,a<b"); for(int b=2; b<=20; b+=2){ int subCountA = 0, subCountB = 0, subCountE = 0; a = (int[])A.clone(); do{ if(a[0]+a[1]> b) { countA++; subCountA++; } if(a[0]+a[1]==b) { countE++; subCountE++; } if(a[0]+a[1]< b) { countB++; subCountB++; } total++; }while(nextComb(a,N,R)); System.out.printf("%2d:%3d,%3d,%3d\n",b,subCountA,subCountE,subCountB); } System.out.println(); g = (countA!=0 ? gcd(countA, total) : 1); System.out.println("P(a> b)= "+countA+"/"+total+"="+(countA/g)+"/"+(total/g)); g = (countB!=0 ? gcd(countE, total) : 1); System.out.println("P(a==b)= "+countE+"/"+total+"="+(countE/g)+"/"+(total/g)); g = (countB!=0 ? gcd(countB, total) : 1); System.out.println("P(a< b)= "+countB+"/"+total+"="+(countB/g)+"/"+(total/g)); tm=System.nanoTime()-tm; // Timer Stop System.out.printf("Runtime : %.3f [sec]\n",(double)tm/1e9); } }
●実行結果
b:a>b,a=b,a<b 2: 45, 0, 0 4: 43, 1, 1 6: 39, 2, 4 8: 33, 3, 9 10: 25, 4, 16 12: 16, 4, 25 14: 9, 3, 33 16: 4, 2, 39 18: 1, 1, 43 20: 0, 0, 45 P(a> b)= 215/450=43/90 P(a==b)= 20/450=2/45 P(a< b)= 215/450=43/90 Runtime : 0.032 [sec]