Showing posts with label Loop Tunnel. Show all posts
Showing posts with label Loop Tunnel. Show all posts

12 May 2017

Candles

My solution:
int candles(int candlesNumber, int makeNew) 
{
    var sum = candlesNumber;
    while (candlesNumber >= makeNew)
    {
        sum += candlesNumber / makeNew;
        candlesNumber = candlesNumber / makeNew + (candlesNumber - candlesNumber / makeNew * makeNew);
    }

    return sum;
}

Previous Next

Rounders

My solution:
int rounders(int value) 
{
    if (value < 10)
        return value;

    var ic = value.ToString().Reverse().ToList();
    var s = "0";
    for (var i = 1; i < ic.Count; i++)
    {
        if (ic[i - 1] > '4')
        {
            if (ic[i] == '9')
                ic.Add('1');

            ic[i] = ic[i] != '9' ? (char) (ic[i] + 1) : '0';
        }

        if (i != ic.Count - 1)
            s += "0";
        else if (ic[i] != '9')
            s += ic[i];
        else
            s += "01";
    }

    return int.Parse(new string(s.Reverse().ToArray()));
}

Previous Next

Increase Number Roundness

My solution:
bool increaseNumberRoundness(int n) 
{
    return n.ToString().TrimEnd('0').Contains('0');
}

Previous Next

Apple Boxes

My solution:
int appleBoxes(int k) 
{
    return Enumerable.Range(1, k).Select(i => Convert.ToInt32(Math.Pow(-1, i) * Math.Pow(i, 2))).Sum();
}

Previous Next

Addition Without Carrying

My solution:
int additionWithoutCarrying(int param1, int param2) 
{
    return (param1 / 10000 + param2 / 10000) % 10 * 10000 + (param1 / 1000 + param2 / 1000) % 10 * 1000 + (param1 / 100 + param2 / 100) % 10 * 100 + (param1 / 10 + param2 / 10) % 10 * 10 + (param1 + param2) % 10;
}

Previous Next

Lineup

My solution:
int lineUp(string commands) 
{
    var b = true;
    var count = 0;
    foreach (var c in commands)
        if ((c == 'L' || c == 'R') && b)
            b = false;
        else if ((c == 'L' || c == 'R') && !b)
        {
            b = true;
            count++;
        }
        else if (c == 'A')
            if (b)
                count++;

    return count;
}

Previous Next

Magical Well

My solution:
int magicalWell(int a, int b, int n) 
{
    var sum = 0;
    while (n != 0)
    {
        sum += a * b;
        a++;
        b++;
        n--;
    }

    return sum;
}

Previous Next

Count Sum Of Two Representations 2

My solution:
int countSumOfTwoRepresentations2(int n, int l, int r) 
{
    var count = 0;
    for (var a = l; a <= r; a++)
        if (a <= n - a && n - a >= l && n - a <= r)
            count++;

    return count;
}

Previous Next

Least Factorial

My solution:
int leastFactorial(int n) 
{
    var i = 1;
    while (Factorial(i) < n)
        i++;

    return Factorial(i);
}

int Factorial(int p0)
{
    if (p0 > 1)
        return Factorial(p0 - 1) * p0;

    return 1;
}

Previous Next