Maximum Value of a Fraction Table

Interview

Time limit1sMemory limit128 MB

Summary
Given a 2x2 grid of positive integers, find how many 90-degree clockwise rotations (0-3) maximize A/C+B/D, choosing the smallest such count on ties.
Level

Easy2 of 10

Topics
Brute force, Implementation, Simulation
Solved
No attempts yet

Problem

Sanggeun is not good at addition or division. Jeongin gave him a 2*2 table made of positive integers A, B, C, and D.

AB
CD

The value of the table is defined as A/C + B/D.

Sanggeun wants to know how many times he should rotate the table clockwise by 90 degrees so that the value becomes as large as possible. The original table is considered to have been rotated 0 times.

After 1 clockwise rotation, the table becomes:

CA
DB

After 2 clockwise rotations, the table becomes:

DC
BA

Given A, B, C, and D, write a program that prints the number of rotations that maximizes the table value.

Input

The first line contains A and B separated by a space. The second line contains C and D separated by a space. Every number is a positive integer at most 100.

Output

Print how many times the table should be rotated to maximize its value. If more than one rotation count gives the maximum value, print the smallest such count.

Examples3

  1. Example 1

    Input
    1 2
    3 4
    
    Expected output
    2
    
  2. Example 2

    Input
    5 9
    7 2
    
    Expected output
    0
    
  3. Example 3

    Input
    41 99
    100 13
    
    Expected output
    1