📜 ⬆️ ⬇️

Entertaining task "Unlucky ticket"

image I think everyone since childhood is familiar with the problem of a happy ticket . However, most often the trip on the bus takes much more time than the time spent on summing the first and last three digits.

And in order to entertain myself until the end of the trip, I invented the concept of “Unlucky Ticket”. A ticket that has no number from the set of values ​​obtained using the first three digits does not match any number from the set of values ​​obtained using the last three digits. Details in the problem statement.

The task

Find all values ​​of 6 digits, for which none of the values ​​of the set obtained from the first three numbers match any value of the set of the last three digits.
')
The set of values ​​for each triad must be obtained:


Example
Ticket: 983060

The set of triad values: 983 [96, 33, 2, 3, 35, 99, 4, 69, 5, 75, 11, 45, 14, 15, 48, 51, 19, 20, 216, 24]
Set of triad values: 060 [0, 6]

There is no general meaning - this is an unlucky ticket.

PS I omit the negative values, since for each negative value there is the same positive

Thus, if you could not find the total number for the first and last three digits by the end of the trip, using the actions from the condition, then you got an unlucky ticket! Or you are not very math.

My solution
I do not see the point of spreading all the code ; I will try to explain the main thing.

A list of all combinations of three numbers - 1000 pieces. Set in which the found tickets will be added.

List<Combination> combinations = new ArrayList<>(1000); Set<String> tickets = new HashSet<>(); 

For each number from the set of values ​​of one combination, check whether there is such a number in the set of values ​​of another combination.

Until the total value is found, we add a line meaning our ticket to the set.
Once there is a value, remove the ticket from the set and proceed to the next comparison.

 for (Combination comb1 : combinations) { for (Combination comb2 : combinations) { for (Integer x : comb1.getValues()) { if (comb2.getValues().contains(x)) { tickets.remove(comb1.toString() + comb2.toString()); break; } else { tickets.add(comb1.toString() + comb2.toString()); } } } } 

I present a method that calculates the set of values ​​for each combination:
(The method is performed for each permutation of 3-digit combination)

  private void countValues(int a, int b, int c) { //Sum addValue(a + b + c); addValue(a + b - c); addValue(a + b * c); addValue((a + b) * c); if (c != 0 && b % c == 0) {addValue(a + b / c);} if (c != 0 && (a + b) % c == 0) { addValue((a + b) / c); } //Subtraction addValue(a - b + c); addValue(a - b - c); addValue(a - b * c); addValue((a - b) * c); if (c != 0 && b % c == 0) {addValue(a - b / c);} if (c != 0 && (a - b) % c == 0) {addValue((a - b) / c);} //Multiplication addValue(a * b + c); addValue(a * b - c); addValue(a * (b - c)); addValue(a * b * c); if (c != 0) { double x = (double)a * (double)b / (double)c; if (isInteger(x)) { addValue((int)x); } } if (c != 0) { double x = (double)a * (double)b / (double)c; if (isInteger(x)) { addValue((int)x); } } //Division if (b != 0 && a % b == 0) { addValue(a / b + c); } if (b + c != 0 && a % (b + c) == 0) { addValue(a / (b + c)); } if (b != 0 && a % b == 0) { addValue(a / b - c); } if (b - c != 0 && a % (b - c) == 0) { addValue(a / (b - c)); } if (b != 0) { double x = (double)a / (double)b * (double)c; if (isInteger(x)) { addValue((int)x); } } if (b != 0 && c != 0) { double x = (double)a / (double)b / (double)c; if (isInteger(x)) { addValue((int)x); } } } 

Total: 23088 tickets.

Lucky ticket: every 18
Unlucky ticket: each 43

Thanks for attention!

Source: https://habr.com/ru/post/312920/


All Articles