Sherlock and The Beast

  • + 0 comments

    In C#

    public static void decentNumber(int n)
    {
        int max5 = 0;
        int max3 = 0;
        // Any number with less than 3 digits
        // isn't Descent 
        if(n < 3)
        {
            Console.WriteLine("-1");
            return;
        }
        
        // If the number can be modded by 3, then
        // we can fill it with fives which will give
        // us the highest value number
        if(n % 3 == 0)
        {
            Console.WriteLine(new string('5', n));
            return;
        }
            
        //Check how many groups of 5's we can put in
        max5 = (n / 3) - 1;
        // Check the number of 3's we can put after them
        max3 = n - (max5 * 3);
        // If the number of 3's doesn't mod by 5 then
        // start removing groups of 5
        if(max3 % 5 != 0)
        {
            // set max3 to zero incase there isn't a number
            // of 5's and 3's that will work
            max3 = 0;
            // While we still have groups of 5's, keep 
            // removing them
            while(max5 > 0)
            {
                // Remove a group of 5's
                max5--;
                // Get the number of remaining spaces
                max3 = n - (max5 * 3);
                // If those can be modded by 5, then
                // we found a good group combo and can
                // exit the loop
                if(max3 % 5 == 0)
                    break;
                // Else set max3 back to 0
                max3 = 0;
            }
        }
        
        // If we ended up with no groups of 5's and 
        // no groups 3's, then no good combination was found
        if(max5 <= 0 && max3 <= 0)
        {
            Console.WriteLine("-1");
            return;
        }
        
        // Print out the group combinations
        Console.Write(new string('5', max5 * 3));
        Console.WriteLine(new string('3', max3));
    }