Determine on which floor the apartment is located

Given the number of apartments (n), entrances (p), floors (q) and the apartment number itself (k). Determine on which floor and in which entrance there is a given apartment under the number k.

A little explanation: n is the number of apartments, i.e. apartments in the entire house. n - the sum of the number of apartments in all entrances.

Therefore, entrances are given - it is not known how many apartments are in 1 entrance. For example, the following data is entered: 18 3 3 16. Here there are 3 floors, 3 entrances, and in each p there are 6 apartments each.

Here is the example data in the form of a table:

1,2 7,8 13,14

3,4 9,10 15,16

5,6 11,12 17,18

The answer in this example will be the numbers 3 and 2. 3 entrance, 2 floor. I think I've made it clear. At least, as I understand this task: https://clck.ru/NcmBu

Here's my attempt. The entrance is always determined correctly, but the problem with the floors is not possible. I tried the usual brute force-not out:

using System;
using System.Linq;

class Program
{
    public class CSharpSrudy2
    {
        static void Main(string[] args)
        {
            string str1 = Console.ReadLine();
            int[] arr = str1.Split(' ').Select(w => Convert.ToInt32(w)).ToArray();
            int n = arr[0], p = arr[1], q = arr[2], k = arr[3];
            int pod = 0, et = 0;
            int i = 0;
            int y = n / p;
            int x = y / q;
            if (k <= y)
            {
                pod = 1;
                while (i <= y)
                {
                    if (k <= i)
                    {
                        break;
                    }
                    et++;
                    i += x;
                }
            }
            if (k > y)
            {
                while (k > y)
                {
                    pod++;
                    y *= 2;
                }
                pod++;
            }
            y = n / p;
            if (k > y)
            {
                for (i = ((pod - 1) * y) + 1; i < pod * y; i += x)
                {
                    if (k < i)
                    {
                        if (k == i)
                        {
                            et++;
                        }
                        break;
                    }
                    et++;
                }
            }
            Console.WriteLine(pod + " " + et);
            Console.ReadLine();
        }
    }
}

2 answers

On the floor there are s = n / (p * q) apartments.

In one entrance of sp = n / p apartments.

Entrance number np = 1 + (k - 1) / sp

Floor number ne = 1 + ((k - 1) % sp) / s

The division is integer.

Check for 30 2 5 27:

s = 3
sp = 15
np = 1 + 26/15 = 2
ne = 1 + (26 % 15) / 3 = 1 + 11/3 = 4

Check for 18 3 3 16:

s = 2
sp = 6
np = 1 + 15/6 = 3
ne = 1 + (15 % 6) / 2 = 1 + 3/2 = 2
 1
Author: MBo, 2020-05-25 16:58:16

Apartments 1..n*q are located in the first entrance, n*q+1..2*n*q in the second, etc. How to use it? Using integer division:
a = (k-1) / (n*q) + 1
Minus one and plus one are here because the numbering starts with 1, not 0.
Next, the apartment number in the entrance is the same, but you need to take the remainder of the division, not the quotient:
b = (k-1) % (n*q) + 1
By analogy, we define the floor:
c = (b-1) / n + 1
Apartment number on the floor:
d = (b-1) % n + 1
It is not clear what the number of entrances p is set for, maybe for boundary condition checks or can there be multiple houses?

 0
Author: Андрей NOP, 2020-05-24 05:57:48