P
P
PRAIT2019-06-15 22:21:32
Java
PRAIT, 2019-06-15 22:21:32

How to correctly implement the definition formula?

Hello guys, there is a task:
There is a nine-story building with 4 entrances. The entrance number starts with one. There are 4 apartments on one floor. Write a program that will receive the apartment number from the keyboard and display on the screen on which floor, which entrance this apartment is located. If there is no such apartment in this house, then you need to inform the user about it.
An example formula is:


X - apartment number 4*9=36
entrance number X div 36 + 1st
floor (X mod 36) div 4 + 1

Need: 21 apartments
4 apartments per floor
9 storey building
4 entrances
4 * 9 = 36 (apartments in the entrance)
(21 - 1) / 36 + 1 = 1; (1 Entrance)
(21 - 1) % 36 / 4 + 1 = 6; (6th Floor)
The meaning is clear, but I don’t understand why subtract and add a unit, please explain or give a link, I will be grateful!
And look what we get:
5-storey building
4 entrances
4 apartments in the entrance
We need 31 apartments
5 * 4 = 20;
(31 - 1) / 20 + 1 \u003d 11 (Entrance)
(31 - 1)% 20 / 4 + 1 \u003d (1st floor)
Something is wrong here, maybe I did not understand how to solve the problem? I will be grateful for your help! Indeed, in this case, we get 31 apartments located on the 3rd floor of the second entrance, right?
Thank you very much for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AltCtrlDel, 2022-02-02
@AltCtrlDel

(21 - 1)% 36 / 4 + 1 = 6; (6th Floor)
Why 4+1 if there are 9 floors
(21 - 1) % 36 / 4 + 1 = 6; (3rd floor)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question