Advertisement
ExtremerBG

SoftUni Advanced Exam - Bakery

Feb 20th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Bakery
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<decimal> water = Console.ReadLine().Split().Select(decimal.Parse).ToList();
  12.             List<decimal> flour = Console.ReadLine().Split().Select(decimal.Parse).ToList();
  13.             List<decimal> nums = new List<decimal>();
  14.             nums.AddRange(water);
  15.             nums.AddRange(flour);
  16.  
  17.             List<decimal> waterLeft = new List<decimal>();
  18.             List<decimal> flourLeft = new List<decimal>();
  19.             int croissantCount = 0;
  20.             int muffinCount = 0;
  21.             int baguetteCount = 0;
  22.             int bagelCount = 0;
  23.  
  24.             while (true)
  25.             {
  26.                 decimal firstN = nums[0];
  27.                 decimal lastN = nums[nums.Count - 1];
  28.                 decimal waterPercentage = (firstN * 100) / (firstN + lastN);
  29.                 decimal flourPercentage = 100 - waterPercentage;
  30.  
  31.                 if (waterPercentage == 50)
  32.                 { // croissant
  33.                     croissantCount++;
  34.                     nums.RemoveAt(0);
  35.                     nums.RemoveAt(nums.Count - 1);
  36.                 }
  37.  
  38.                 else if (waterPercentage == 40)
  39.                 { // muffin
  40.                     muffinCount++;
  41.                     nums.RemoveAt(0);
  42.                     nums.RemoveAt(nums.Count - 1);
  43.                 }
  44.  
  45.                 else if (waterPercentage == 30)
  46.                 { // baguette
  47.                     baguetteCount++;
  48.                     nums.RemoveAt(0);
  49.                     nums.RemoveAt(nums.Count - 1);
  50.                 }
  51.  
  52.                 else if (waterPercentage == 20)
  53.                 { // bagel
  54.                     bagelCount++;
  55.                     nums.RemoveAt(0);
  56.                     nums.RemoveAt(nums.Count - 1);
  57.                 }
  58.  
  59.                 else
  60.                 {
  61.                     decimal removeDifference = firstN - lastN;
  62.                     removeDifference = Math.Abs(removeDifference);
  63.  
  64.                     if (firstN > lastN)
  65.                     { // water bigger
  66.                         firstN -= removeDifference;
  67.                         croissantCount++;
  68.                         waterLeft.Add(removeDifference);
  69.                     }
  70.  
  71.                     else if (lastN > firstN)
  72.                     { // flour bigger
  73.                         lastN -= removeDifference;
  74.                         croissantCount++;
  75.                         flourLeft.Add(removeDifference);
  76.                     }
  77.  
  78.                     nums.RemoveAt(0);
  79.                     nums.RemoveAt(nums.Count - 1);
  80.                 }
  81.  
  82.                 if (nums.Count == 1)
  83.                 {
  84.                     if (flour.Count > water.Count)
  85.                     {
  86.                         flourLeft.Add(nums[0]);
  87.                         break;
  88.                     }
  89.  
  90.                     if (water.Count > flour.Count)
  91.                     {
  92.                         waterLeft.Add(nums[0]);
  93.                         break;
  94.                     }
  95.                 }
  96.  
  97.                 else if (nums.Count == 0)
  98.                 {
  99.                     break;
  100.                 }
  101.             }
  102.  
  103.             SortedList<string, int> counts = new SortedList<string, int>();
  104.  
  105.             bool croissantZero = biggerThanZero(croissantCount);
  106.             if (croissantZero)
  107.             {
  108.                 counts.Add("Croissant", croissantCount);
  109.             }
  110.  
  111.             bool muffinZero = biggerThanZero(muffinCount);
  112.             if (muffinZero)
  113.             {
  114.                 counts.Add("Muffin", muffinCount);
  115.             }
  116.  
  117.             bool baguetteZero = biggerThanZero(baguetteCount);
  118.             if (baguetteZero)
  119.             {
  120.                 counts.Add("Baguette", baguetteCount);
  121.             }
  122.  
  123.             bool bagelZero = biggerThanZero(bagelCount);
  124.             if (bagelZero)
  125.             {
  126.                 counts.Add("Bagel", bagelCount);
  127.             }
  128.  
  129.             printProducts(counts);
  130.  
  131.             if (waterLeft.Count > 0)
  132.             {
  133.                 Console.Write("Water left: ");
  134.                 Console.Write(String.Join(", ", waterLeft));
  135.             }
  136.  
  137.             else
  138.             {
  139.                 Console.WriteLine("Water left: None");
  140.             }
  141.  
  142.             if (flourLeft.Count > 0)
  143.             {
  144.                 Console.Write("Flour left: ");
  145.                 Console.Write(string.Join(", ", flourLeft));
  146.             }
  147.  
  148.             else
  149.             {
  150.                 Console.WriteLine("Flour left: None");
  151.             }
  152.         }
  153.  
  154.         public static bool biggerThanZero(int n)
  155.         {
  156.             if (n > 0)
  157.             {
  158.                 return true;
  159.             }
  160.  
  161.             else
  162.             {
  163.                 return false;
  164.             }
  165.         }
  166.         public static void printProducts(SortedList<string, int> counts)
  167.         {
  168.             foreach (KeyValuePair<string, int> count in counts.OrderByDescending(key => key.Value))
  169.             {
  170.                 Console.WriteLine($"{count.Key}: {count.Value}");
  171.             }
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement