java - Method that walks through combinations and counting those who passes condition -
i should find every 5-number combination of values 0 1 2 3 4 5 0 not first , 1 digit not repeated more 1 time.the method shall return number of valid combinations.
valid combinations:
1 0 0 2 1 1 2 3 4 5 1 1 2 2 3
invalid combinations:
0 1 2 3 4 (0 cant first) 2 3 3 3 5 (not allowed 3 of same digits) 0 1 2 3 4 5 (6 digit numbers not allowed)
i did similar task permutations, i'm not quite sure how approach one. right answer 5100 (i solved mathematically).
you need write isvalid function , try following pseudo-code
nb_solution = 0 = 0 ; < 100000; i++ if isvalid(i) nb_solution ++
edit : did, , find same value found.
public static void computeexample() { int count = 0; for(int = 0; < 100000; i++) { if(isvalid(i)) { count++; } } system.out.println(count); } public static int[] splitciffers(int number) { int tmp = number; int unite = tmp % 10; tmp = (tmp - unite) / 10; int dizaine = tmp % 10; tmp = (tmp - dizaine) / 10; int centaine = tmp % 10; tmp = (tmp - centaine) / 10; int millier = tmp % 10; tmp = (tmp - millier) / 10; int dix_millier = tmp % 10; int[] retour = new int [5]; retour[0] = dix_millier; retour[1] = millier; retour[2] = centaine; retour[3] = dizaine; retour[4] = unite; return retour; } public static boolean isvalid(int number) { boolean isvalid = true; int[] digitbydigit = splitciffers(number); int[] digitallowed = new int[6]; for(int = 0; < 6; i++) { digitallowed[i] = 0; } if(digitbydigit[0] == 0) { isvalid = false; } for(int = 0; < 5; i++) { if(digitbydigit[i] < 0 || digitbydigit[i] > 5) { isvalid = false; } else { digitallowed[digitbydigit[i]]++; } } for(int = 0; < 6; i++) { if(digitallowed[i] > 2) { isvalid = false; } } return isvalid; }
Comments
Post a Comment