Phone Call

Description:

Some phone usage rate may be described as follows:

  • first minute of a talk costs min1 cents,
  • each minute from the 2nd up to 10th (inclusive) costs min2_10 cents
  • each minute after 10th costs min11 cents.

You have S cents on your account before the call. What is the duration of the longest call (in minutes rounded down to the nearest integer) you can have?

Example

For min1 = 3, min2_10 = 1, min11 = 2 and S = 20, the output should be
phoneCall(min1, min2_10, min11, S) = 14.

Here’s why:

  • the first minute costs 3 cents, which leaves you with 20 - 3 = 17 cents;
  • the total cost of minutes 2 through 10 is 1 * 9 = 9, so you can talk 9 more minutes and still have 17 - 9 = 8 cents;
  • each next minute costs 2 cents, which means that you can talk 8 / 2 = 4 more minutes.

Thus, the longest call you can make is 1 + 9 + 4 = 14 minutes long.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer min1

    Constraints:
    1 ≤ min1 ≤ 10.

  • [input] integer min2_10

    Constraints:
    1 ≤ min2_10 ≤ 10.

  • [input] integer min11

    Constraints:
    1 ≤ min11 ≤ 10.

  • [input] integer S

    Constraints:
    2 ≤ S ≤ 60.

  • [output] integer

Tests:

  • Test1
    Input:
    min1: 3
    min2_10: 1
    min11: 2
    S: 20
    Expected Output:
    14
  • Test2
    Input:
    min1: 2
    min2_10: 2
    min11: 1
    S: 2
    Expected Output:
    1
  • Test3
    Input:
    min1: 10
    min2_10: 1
    min11: 2
    S: 22
    Expected Output:
    11
  • Test4
    Input:
    min1: 2
    min2_10: 2
    min11: 1
    S: 24
    Expected Output:
    14
  • Test5
    Input:
    min1: 1
    min2_10: 2
    min11: 1
    S: 6
    Expected Output:
    3

Solution(C#):

int phoneCall(int min1, int min2_10, int min11, int S) {
    if(min1 >= 1  && min1 <= 10 &&
      min2_10 >= 1 && min2_10 <= 10 &&
      min11 >= 1 && min11 <= 10 &&
      S >= 2 && S <= 60){
        int t1 = S/min1;
        if(t1>1){            
            int t2 = (S - min1)/min2_10;
            if(t2>=9){
                return 10+(S - min1 - 9*min2_10)/min11;
            }else{
                return 1+t2;
            }
        }
        else{
            return t1;
        }
        return S-min1-min2_10-min11;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}
Advertisements

Late Ride

Description:

One night you go for a ride on your motorcycle. At 00:00 you start your engine, and the built-in timer automatically begins counting the length of your ride, in minutes. Off you go to explore the neighborhood.

When you finally decide to head back, you realize there’s a chance the bridges on your route home are up, leaving you stranded! Unfortunately, you don’t have your watch on you and don’t know what time it is. All you know thanks to the bike’s timer is that n minutes have passed since 00:00.

Using the bike’s timer, calculate the current time. Return an answer as the sum of digits that the digital timer in the format hh:mm would show.

Example

  • For n = 240, the output should be
    lateRide(n) = 4.

    Since 240 minutes have passed, the current time is 04:00. The digits sum up to 0 + 4 + 0 + 0 = 4, which is the answer.

  • For n = 808, the output should be
    lateRide(n) = 14.

    808 minutes mean that it’s 13:28 now, so the answer should be 1 + 3 + 2 + 8 = 14.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer n

    The duration of your ride, in minutes. It is guaranteed that you’ve been riding for less than a day (24 hours).

    Constraints:
    0 ≤ n < 60 · 24.

  • [output] integer

    The sum of the digits the digital timer would show.

Tests:

  • Test1
    Input:
    n: 240
    Expected Output:
    4
  • Test2
    Input:
    n: 808
    Expected Output:
    14
  • Test3
    Input:
    n: 1439
    Expected Output:
    19
  • Test4
    Input:
    n: 0
    Expected Output:
    0
  • Test5
    Input:
    n: 23
    Expected Output:
    5
  • Test6
    Input:
    n: 8
    Expected Output:
    8

Solution(C#):

int lateRide(int n) {
    if(n>=0 && n<60*24){
        int first = n/60;
        int second = n%60;
        return first/10+first%10+second/10+second%10;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}

Circle of Numbers

Description:

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n - 1 are neighbouring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.

Example

For n = 10 and firstNumber = 2, the output should be
circleOfNumbers(n, firstNumber) = 7.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer n

    A positive even integer.

    Constraints:
    4 ≤ n ≤ 20.

  • [input] integer firstNumber

    Constraints:
    0 ≤ firstNumber ≤ n - 1.

  • [output] integer

Tests:

  • Test1
    Input:
    n: 10
    firstNumber: 2
    Expected Output:
    7
  • Test2
    Input:
    n: 10
    firstNumber: 7
    Expected Output:
    2
  • Test3
    Input:
    n: 4
    firstNumber: 1
    Expected Output:
    3
  • Test4
    Input:
    n: 6
    firstNumber: 3
    Expected Output:
    0

Solution(C#):

int circleOfNumbers(int n, int firstNumber) {
    if(n >= 4 && n <=20 &&
      firstNumber >= 0 && firstNumber <= n-1){
        return (firstNumber +n/2)%n;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}

Max Multiple

Description:

Given a divisor and a bound, find the largest integer N such that:

  • N is divisible by divisor.
  • N is less than or equal to bound.
  • N is greater than 0.

It is guaranteed that such a number exists.

Example

For divisor = 3 and bound = 10, the output should be
maxMultiple(divisor, bound) = 9.

The largest integer divisible by 3 and not larger than 10 is 9.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer divisor

    Constraints:
    2 ≤ divisor ≤ 10.

  • [input] integer bound

    Constraints:
    5 ≤ bound ≤ 100.

  • [output] integer

    The largest integer not greater than bound that is divisible by divisor.

Tests:

  • Test1
    Input:
    divisor: 3
    bound: 10
    Expected Output:
    9
  • Test2
    Input:
    divisor: 2
    bound: 7
    Expected Output:
    6
  • Test3
    Input:
    divisor: 10
    bound: 50
    Expected Output:
    50
  • Test4
    Input:
    divisor: 7
    bound: 100
    Expected Output:
    98

Solution(C#):

int maxMultiple(int divisor, int bound) {
    if(divisor <= 10 && divisor >= 2 &&
      bound <= 100 && bound >= 5){
        return (bound/divisor) * divisor;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}

Seats in Theater

Description:

Your friend advised you to see a new performance in the most popular theater in the city. He knows a lot about art and his advice is usually good, but not this time: the performance turned out to be awfully dull. It’s so bad you want to sneak out, which is quite simple, especially since the exit is located right behind your row to the left. All you need to do is climb over your seat and make your way to the exit.

The main problem is your shyness: you’re afraid that you’ll end up blocking the view (even if only for a couple of seconds) of all the people who sit behind you and in your column or the columns to your left. To gain some courage, you decide to calculate the number of such people and see if you can possibly make it to the exit without disturbing too many people.

Given the total number of rows and columns in the theater (nRows and nCols, respectively), and the row and column you’re sitting in, return the number of people who sit strictly behind you and in your column or to the left, assuming all seats are occupied.

Example

For nCols = 16, nRows = 11, col = 5 and row = 3, the output should be
seatsInTheater(nCols, nRows, col, row) = 96.

Here is what the theater looks like:

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer nCols

    An integer, the number of theater’s columns.

    Constraints:
    1 ≤ nCols ≤ 1000.

  • [input] integer nRows

    An integer, the number of theater’s rows.

    Constraints:
    1 ≤ nRows ≤ 1000.

  • [input] integer col

    An integer, the column number of your own seat (1-based).

    Constraints:
    1 ≤ col ≤ nCols.

  • [input] integer row

    An integer, the row number of your own seat (1-based).

    Constraints:
    1 ≤ row ≤ nRows.

  • [output] integer

    The number of people who sit strictly behind you and in your column or to the left.

Tests:

  • Test1
    Input:
    nCols: 16
    nRows: 11
    col: 5
    row: 3
    Expected Output:
    96
  • Test2
    Input:
    nCols: 1
    nRows: 1
    col: 1
    row: 1
    Expected Output:
    0
  • Test3
    Input:
    nCols: 13
    nRows: 6
    col: 8
    row: 3
    Expected Output:
    18
  • Test4
    Input:
    nCols: 60
    nRows: 100
    col: 60
    row: 1
    Expected Output:
    99
  • Test5
    Input:
    nCols: 1000
    nRows: 1000
    col: 1000
    row: 1000
    Expected Output:
    0

Solution(C#):

int seatsInTheater(int nCols, int nRows, int col, int row) {
    if(nCols >= 1 && nCols <= 1000 &&
      nRows >= 1 && nRows <= 1000 &&
      col >= 1 && col <= nCols &&
      row >= 1 && row <= nRows){
        return (nCols - col + 1) * (nRows - row);
    }else{
        throw new ArgumentOutOfRangeException();
    }
}

Candies

Description:

n children have got m pieces of candy. They want to eat as much candy as they can, but each child must eat exactly the same amount of candy as any other child. Determine how many pieces of candy will be eaten by all the children together. Individual pieces of candy cannot be split.

Example

For n = 3 and m = 10, the output should be
candies(n, m) = 9.

Each child will eat 3 pieces. So the answer is 9.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer nThe number of children.

    Constraints:
    1 ≤ n ≤ 10.

  • [input] integer mThe number of pieces of candy.

    Constraints:
    2 ≤ m ≤ 100.

  • [output] integerThe total number of pieces of candy the children will eat provided they eat as much as they can and all children eat the same amount.

Tests:

  • Test1
    Input:
    n: 3
    m: 10
    Expected Output:
    9
  • Test2
    • Input:
      n: 1
      m: 2
      Expected Output:
      2

Solution(C#):

int candies(int n, int m) {
    if(n>=1 && n<=10 && m>=2 && m<=100){
        return (m/n) * n;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}

Largest Number

Description:

Given an integer n, return the largest number that contains exactly n digits.

Example

For n = 2, the output should be
largestNumber(n) = 99.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer nConstraints:
    1 ≤ n ≤ 7.
  • [output] integerThe largest integer of length n.

Tests:

  • Test1
    Input:
    n: 2
    Expected Output:
    99
  • Test2
    Input:
    n: 1
    Expected Output:
    9

Solution(C#):

int largestNumber(int n) {
    if(n>=1 && n<=7){
        return (int)Math.Pow(10,n)-1;
    }else{
        throw new ArgumentOutOfRangeException();
    }
}