12 May 2017

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