本帖最後由 vichui 於 2016-3-26 14:12 編輯
回覆 2# hihihi123hk
冇眼睇,用Brute Force去做呢題, 可以話係最差的方法。。。
完全冇用到重要的資料去做。。。
1. P=1
2. F+H =11 所以只會有 2+9,3+8,4+7,5+6,6+5,7+4,8+3,9+2
3.E+G = 10 所以只會有 2+8,3+7,4+6,6+4,7+3,8+2
由於不能有相同====>
E+G = 2+8,F+H -> 4+7,5+6,6+5,7+4
= 3+7,F+H -> 2+9,5+6,6+5,9+2
= 4+6 , F+H -> 2+9,3+8,8+3,9+2
= 6+4 , F+H -> 2+9,3+8,8+3,9+2
= 7+3, F+H -> 2+9,5+6,6+5,9+7
= 8+2, F+H -> 4+7,5+6,6+5,7+4
代入後,可能的EF-GH 就會係
EF 24 25 26 27 32 35 36 39 42 43 48 49
GH 87 86 85 84 79 76 75 72 69 68 63 62
EF 87 86 85 84 79 76 75 72 69 68 63 62
GH 24 25 26 27 32 35 36 39 42 43 48 49
每組可能數字的ABCD數字為:
35 34 34 35 45 24 24 45 35 25 25 35
69 79 79 69 68 89 89 68 78 79 79 78
e.g. EF=24/87 ABCD 就會係 (3,5,6,9) 組成的, 由於A > C 所以
A-C=2==> 5-3 ==> test 56-39/59-36=24?? ==> No
如果B-D要,借位(A-1)-C=2=> A-C=3 ==> 9-6/6-3== (B <D) => 95 -63/69-35=24??? ==> No
Test Cases 大約係 24* 3*2*2~~ 288
最後。。Coding 唔算太齊整- public class Solver {
- static int count=0;
- static int[] EF={24,25,26,27,32,35,36,39,42,43,48,49,87, 86, 85, 84, 79, 76, 75, 72, 69, 68, 63, 62};
- static int[] GH={87, 86, 85, 84, 79, 76, 75, 72, 69, 68, 63, 62,24,25,26,27,32,35,36,39,42,43,48,49};
- static int[][] ABCD={
- {3,5,6,9},
- {3,4,7,9},
- {3,4,7,9},
- {3,5,6,9},
- {4,5,6,8},
- {2,4,8,9},
- {2,4,8,9},
- {4,5,6,8},
- {3,5,7,8},
- {2,5,7,9},
- {2,5,7,9},
- {3,5,7,8},
- {3,5,6,9},
- {3,4,7,9},
- {3,4,7,9},
- {3,5,6,9},
- {4,5,6,8},
- {2,4,8,9},
- {2,4,8,9},
- {4,5,6,8},
- {3,5,7,8},
- {2,5,7,9},
- {2,5,7,9},
- {3,5,7,8},
- };
- public static void main(String[] args) {
- for (int i =0 ; i < EF.length ; i++){
- testAC(EF[i],GH[i],ABCD[i]);
- }
- }
- private static void testAC(int ef,int gh, int[] abcd) {
- // test A-C=E where A > C
- int e = ef/10;
- int f = ef-e*10;
- for (int i =3 ; i >=0 ; i--){
- for (int j=i-1;j >=0 ; j--){
- if(abcd[i]-abcd[j]==e){
- int[] canidate = new int[4];
- System.arraycopy(abcd, 0, canidate , 0, abcd.length);
- swap(canidate,3,i);
- swap(canidate,2,j);
- if (((canidate[3]*10 + canidate[0]) - (canidate[2]*10 + canidate[1])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 + canidate[0],canidate[2]*10 + canidate[1],ef,gh);
- }
- if (((canidate[3]*10 + canidate[1]) - (canidate[2]*10 + canidate[0])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 + canidate[1],canidate[2]*10 + canidate[0],ef,gh);
- }
- }
- }
- }
- //test A-C=E+1
- for (int i =3 ; i >=0 ; i--){
- for (int j=i-1;j >=0 ; j--){
- if(abcd[i] - 1 -abcd[j]==e){
- int[] canidate = new int[4];
- System.arraycopy(abcd, 0, canidate , 0, abcd.length);
- swap(canidate,3,i);
- swap(canidate,2,j);
- if (((canidate[3]*10 + canidate[0]) - (canidate[2]*10 + canidate[1])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 + canidate[0],canidate[2]*10 + canidate[1],ef,gh);
- }
- if (((canidate[3]*10 + canidate[1]) - (canidate[2]*10 + canidate[0])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 + canidate[1],canidate[2]*10 + canidate[0],ef,gh);
- }
- if (((canidate[3]*10 + 0) - (canidate[2]*10 + canidate[1])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 ,canidate[2]*10 + canidate[1],ef,gh);
- }
- if (((canidate[3]*10 + 0) - (canidate[2]*10 + canidate[0])) == ef){
- System.out.printf("%d: %d - %d = %d + %d = 111 \n",count++,canidate[3]*10 ,canidate[2]*10 + canidate[0],ef,gh);
- }
- }
- }
- }
- }
-
- public static void swap(int[] array,int i, int j ) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
複製代碼 ---> output :
0: 90 - 63 = 27 + 84 = 111
1: 86 - 54 = 32 + 79 = 111
2: 85 - 46 = 39 + 72 = 111
3: 95 - 27 = 68 + 43 = 111
4: 90 - 27 = 63 + 48 = 111 |