public string DiceGame2(int m, int n, int target)
        {
            int  totalSides     = m + n;
            bool isNoWay        = totalSides < target; // The sides must add up to at least the target.
            bool isInvalidInput = totalSides < 2 || target < 2;

            int maxWays = target - 1;
            int numWays;

            if (isInvalidInput)
            {
                numWays = -1;
            }
            else if (isNoWay)
            {
                numWays = 0;
            }
            else
            {
                // The number of ways is limited by the smaller number of sides.
                numWays = Math.Min(m, n);

                // I'm not sure how to explain it, but if both m and n are less than the target,
                // it works out that if you add them together and subtract (target - 1) you get the right number.
                numWays = Math.Min(numWays, totalSides - maxWays);

                // No matter what, the number of ways can never be more than target - 1.
                numWays = Math.Min(numWays, maxWays);
            }

            return(OutputStringCreator.getOutputString(numWays, target));
        }
Пример #2
0
        public string Menu(int burger, int drink, int side, int dessert)
        {
            int    calories     = CalorieCalculator.getCalories(burger, drink, side, dessert);
            string outputString = OutputStringCreator.getOutputString(calories);

            return(outputString);
        }
        public string DiceGameN2(int m, int n, int target)
        {
            // Check every side of m against every side of n. If the sides add up to the
            // target, increment the count.
            int numWays = 0;

            for (int i = 1; i <= m; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i + j == target)
                    {
                        numWays++;
                    }
                }
            }

            return(OutputStringCreator.getOutputString(numWays, target));
        }