Search code examples
c#decimalulong

c# decimal, ulong, calculating quantities


I have this code for my problem.I am almost there by for some of the tests I get slightly different results. As you will see,variable c should be "decimal". However when multiplying it by variable "n". This is the problem:

Your task is to write a program to calculate the amount of cakes that Ivancho can make that day and the price forone cake. You’ll be given some numbers. The number of cakes Ivancho wants that day, the kilograms of flour needed to make one cake, the kilograms of flour which the provider can give you, the amount of truffles you canbuy and the price for each truffle. If Ivancho has enough flour to make the amount of cakes he wants, he will make exactly that amount, the leftover flour will be discarded and you should print on the console “All products available, price of a cake: {price of one cake}”. Alternatively if there is not enough flour you should round down the number of cakes that can be produced to a whole number and print them on the console in the format “Can make only {number of cakes that can be produced} cakes, need {kilograms of flour needed} kg more flour” where kilograms of flour needed is the difference between the kilograms of flour required to make the amount of cakes Ivancho wanted and the kilograms of flour available. Input The input data should be read from the console. It consists of five input values, each at a separate line: • The number n – amount of cakes Ivancho wants. • The number c – kilograms of flour needed to make one cake. • The number f – kilograms of flour available. • The number t – amount of truffles available. • The number p – price of one truffle.

 If there isn’t enough flour to make the amount of cakes Ivancho wants print on the console: o “Can make only {number of cakes that can be produced} cakes, need {kilograms of flour needed} kg more flour”  If there is enough flour:  The number of cakes that can be produced must be a whole number; the price of the cake and the kilograms of flour needed must be rounded to two digits after the decimal point. Constraints  The number n will be a valid integer in the range [1 … 18 446 744 073 709 551 615]  The number c will be a floating-point numbers in the range [0 … 7.9 x 1028].  The numbers f, t and p will be valid integers in the range [0 ... 4 294 967 295].  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace CakeTycoon
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ulong.Parse(Console.ReadLine()); // amount of cakes George wants
            var c = decimal.Parse(Console.ReadLine()); // kg of fllour needed to make 1 cake

            var f = uint.Parse(Console.ReadLine()); // kg of flour available
            var t = uint.Parse(Console.ReadLine()); // amount of truffles available
            var p = uint.Parse(Console.ReadLine()); // price of one truffle


            var truffelCost = t * p; // total price of truffles

            var cakesCanBeMade = Math.Floor(f / c );

            var cakePrice = (truffelCost / n) * 1.25;
            var flourNeeded = 0.00;
            var totalFlour = n * c;

            if (cakesCanBeMade >= n)
            {
                Console.WriteLine("All products available, price of a cake: {0}", cakePrice); 

            }
            else if (cakesCanBeMade < n)
            {
                flourNeeded = totalFlour - f ;
                Console.WriteLine("Can make only {0} cakes, need {1}kg more flour", cakesCanBeMade,flourNeeded);
            }

        }
    }
}

Solution

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CakeTycoon
    {
        class Program
        {
            static void Main(string[] args)
            {
                ulong cakesWanted = ulong.Parse(Console.ReadLine());
                double kilosPerCake = double.Parse(Console.ReadLine());
                uint flourKilos = uint.Parse(Console.ReadLine());
                uint truffles = uint.Parse(Console.ReadLine());
                uint trufflePrice = uint.Parse(Console.ReadLine());
    
                ulong truffleCost = (ulong)truffles * trufflePrice;
                double cakesProduced = Math.Floor(flourKilos / kilosPerCake);
    
                if (cakesProduced < cakesWanted)
                {
                    double kilogramsNeeded = (kilosPerCake * cakesWanted) - flourKilos;
                    Console.WriteLine("Can make only {0} cakes, need {1:F2} kg more flour", cakesProduced, kilogramsNeeded);
    
                }
                else
                {
                    double cakeCost = ((double)truffleCost / cakesWanted) * 1.25d;
                    Console.WriteLine("All products available, price of a cake: {0:F2}", cakeCost);
                }
            }
        }
    }