Halloween Sale, by Hackerrank

Easy problem, here: https://www.hackerrank.com/challenges/halloween-sale/problem:

You wish to buy video games from the famous online video game store Mist.
Usually, all games are sold at the same price,  dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game you buy during the sale will be sold at  dollars, but every subsequent game you buy will be sold at exactly  dollars less than the cost of the previous one you bought. This will continue until the cost becomes less than or equal to  dollars, after which every game you buy will cost  dollars each.
For example, if  and , then the following are the costs of the first  games you buy, in order:
You have  dollars in your Mist wallet. How many games can you buy during the Halloween Sale?
Notice that the problem description is the algorithm that you want to implement, and given the small input limits, a simple brute-force would do it. Just follow the description line by line, and have fun! Cheers, Marcelo.



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static int howManyGames(int p, int d, int m, int s)
{
int tickets = 0;
int cost = 0;
while (s >= 0)
{
if (tickets == 0)
{
cost = p;
}
else
{
cost -= d;
if (cost < m)
{
cost = m;
}
}
s -= cost;
if (s >= 0)
{
tickets++;
}
}

return tickets;
}


    static void Main(String[] args) {
        string[] tokens_p = Console.ReadLine().Split(' ');
        int p = Convert.ToInt32(tokens_p[0]);
        int d = Convert.ToInt32(tokens_p[1]);
        int m = Convert.ToInt32(tokens_p[2]);
        int s = Convert.ToInt32(tokens_p[3]);
        int answer = howManyGames(p, d, m, s);
        Console.WriteLine(answer);
    }
}

Comments

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

The Power Sum, a recursive problem by HackerRank