A
A
Almost2021-04-29 01:00:46
Algorithms
Almost, 2021-04-29 01:00:46

How to find nearest integer to real, excluding integers from list?

Given a real number, let's say float.
It is necessary to find the nearest integer that will not be in a separate list.

Examples:
1) There is a number 4.6. In a separate list, the number 5. The result should be 4.
2) There is a number 4.6. In a separate list of numbers 5, 4. The result should be 6.
3) There is a number 4.6. In a separate list, the numbers are 3, 4, 5, 6, 7. The result should be 2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-04-29
@Almost

You round the number, check if it is in the array.
If not, the number is found.
If so, add/subtract from the number 1 and check again if it is in the array.
If not, the number is found.
If so, subtract / add 2 to the number and check again if it is in the array.
If not, the number is found.
If so, you add/subtract from the number 3 and check again if it is in the array.
Etc.
You alternate addition with subtraction (start depending on where it was rounded off) and at each iteration you increase the summand-subtracted by 1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question