-1

Nate likes to use elevator while being at a building. On his father’s office, there are 3 identical elevators (which have same velocity 1 level/second). Unfortunately, one of those elevators is in maintenance. Then, when Nate sees those two elevators, the first one is on the x floor going up and the second one is on the y floor and is going down. The third elevator (which in maintenance) is recently on the k floor. Nate is wondering if those three elevators will be in the same level or not.

Format input: Input consists of 3 integers x, y, k, the position of the first, second, and third elevator, respectively.

Format output: Output a line consists of t which describe the time when the three elevators are on the same levels. If this event is impossible to happen, output -1.

Constraints: 0 ≤ x, y, k ≤ 100

Sample input 1 (user input):

3 5 4

Sample output:

1

Sample input 2 (user input):

1 2 5

Sample output:

-1

This is my code:

#include <stdio.h>

int main()
{
    int x, y ,k;
    scanf("%d %d %d", &x, &y, &k);
    
    while(x >= k && y <= k)
    {
        if(x-k == k-y || k-x == y-k)
        {
            if(x < k)
            {
                printf("%d\n", k-x);
            }
            else if(k <= x)
            {
                printf("%d\n", x-k);
            }
        }
        else if(x == y)
        {
            if(x > k)
            {
                printf("%d\n", x-k);
            }
            else if(x <= k)
            {
                printf("%d\n", k-x);
            }
        }
        else
        {
            printf("-1\n");
        }
        x++;
        y--;
    }
    
    return 0;
}

My answer is incorrect when I submit it to the Online Judge, I think it's because if I input 2 6 4, the output is supposed to be 2, since they need 2 seconds for all the elevators to be on the same level, but my code's output is -1. Can anyone help me with this problem?

  • If you know the input that causes your logic to fail, then you should build the program locally and use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line (while monitoring variables and their values) to see what happens and where your logic might fail. – Some programmer dude Oct 13 '22 at 11:39
  • On another note, if `x > k` is false, then `x <= k` is guaranteed to be true. You don't need an `if (x <= k)`. – Some programmer dude Oct 13 '22 at 11:41
  • This problem can be solved either using mathematics formula or an algorithm that compute the elevators movement. It seems that you are mixing the two. As Some programmer dude stated, try using a debugger, see what happen step by step and reconsider your approch – DrosvarG Oct 13 '22 at 11:42
  • Either the code you posted not the same as the code you are running or you did not actually enter `2 6 4` to get a `-1` ouput.. i.e. If you entered `2 6 4` into `scanf("%d %d %d", &x, &y, &k);` then the code would immediately break out of the loop and exit because in your loop: `while(x >= k && y <= k)`, both expressions: `x >= k` and `y <= k` are `false`. – ryyker Oct 13 '22 at 12:56

1 Answers1

0

"if I input 2 6 4, the output is supposed to be 2, ..., but my code's output is -1. Can anyone help ..."

In the code posted the following lines will result in exiting the loop if 2 6 4 is entered:

scanf("%d %d %d", &x, &y, &k);//eg entered 2 6 4

while(x >= k && y <= k)// both 2 >= 4 and 6 <= 4 are false

If the `while conditions are change to:

while(x <= k && y >= k)//reverse the conditional evaluators
        ^         ^

2 6 4 will evaluate properly

And the following expression will output the correct value:

if(x < k)
{
    printf("%d\n", k-x);
}

Some suggestions

  • Learn to debug by running code in a debug environment, one that will allow you to step through your code to inspect variable values by using break points, and to observe execution flow one line at a time. Additionally many believe talking through a problem with a rubber duck will help.

  • For a problem like this it is important to fully understand the problem before attempting to code a solution. Use visualization by drawing a picture or a flowchart. Or re-write the problem description using your own words. Anything to fully understand the problem. Something as simple as this for example:

    enter image description here

  • Without being terse to the point of obfuscation, code what is needed in the fewest statements practical. In this case your problem statement describes a single output for a set of inputs. So there is no need to iterate unless it was specified by the problem statement. So eliminate the while loop. And it is very possible that the problem can be solved without so many if-else clauses.

To illustrate - 3 elevators with only two in motion, both traveling at the same rate but in opposite direction can be reduced to evaluating two distances. Note that same rate equates to distance 1 == distance 2 supporting criteria that elevators must arrive at the floor of the static elevator at the same time.

//after inputting values for x, y and k:
int d1 = k - x;//static elevator floor minus the up elevator floor
int d2 = y - k;//down elevator floor minus the static elevator floor
if(d1 > 0 && d1 - d2 == 0)//d1 == d2 and both > 0
{
    printf("%d\n", d1);
}
else
{
     printf("%d\n", -1); 
}
return 0;
...
ryyker
  • 22,849
  • 3
  • 43
  • 87