Advertisement
gabi11

Demo Exam - 02. Make a Salad (80%)

Jun 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DemoExam16062019
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Queue<string> vegetables = new Queue<string>(Console.ReadLine().Split());
  12.  
  13.             Stack<int> calories = new Stack<int>(Console.ReadLine()
  14.                                              .Split()
  15.                                              .Select(int.Parse));
  16.  
  17.             var clone = new Stack<int>(calories.Reverse());
  18.  
  19.             List<int> readySalads = new List<int>();
  20.             List<string> remainingItem = new List<string>();
  21.  
  22.             int caloriesToRemove = 0;
  23.  
  24.             while (true)
  25.             {
  26.  
  27.                 if (vegetables.Count == 0 || calories.Count == 0)
  28.                 {
  29.                     break;
  30.                 }
  31.  
  32.                 string vegetable = vegetables.Peek();
  33.  
  34.                 switch (vegetable)
  35.                 {
  36.                     case "tomato":
  37.                         caloriesToRemove = 80;
  38.                         break;
  39.  
  40.                     case "carrot":
  41.                         caloriesToRemove = 136;
  42.                         break;
  43.  
  44.                     case "lettuce":
  45.                         caloriesToRemove = 109;
  46.                         break;
  47.  
  48.                     case "potato":
  49.                         caloriesToRemove = 215;
  50.                         break;
  51.                 }
  52.  
  53.                 int caloriesNeeded = clone.Peek();
  54.                 int currentCalories = caloriesNeeded - caloriesToRemove;
  55.  
  56.                 if (currentCalories >= 0)
  57.                 {
  58.                     vegetables.Dequeue();
  59.                     clone.Pop();
  60.                     clone.Push(currentCalories);
  61.                 }
  62.  
  63.  
  64.                 else if (currentCalories < 0)
  65.                 {
  66.                     vegetables.Dequeue();
  67.                     clone.Pop();
  68.                     readySalads.Add(calories.Peek());
  69.                     calories.Pop();
  70.                 }
  71.  
  72.                 currentCalories = 0;
  73.             }
  74.  
  75.             while (true)
  76.             {
  77.                 if (vegetables.Count == 0 && calories.Count == 0)
  78.                 {
  79.                     break;
  80.                 }
  81.  
  82.                 if (vegetables.Any())
  83.                 {
  84.                     remainingItem.Add(vegetables.Dequeue());
  85.                 }
  86.  
  87.                 else if (calories.Any())
  88.                 {
  89.                     remainingItem.Add(calories.Pop().ToString());
  90.                 }
  91.             }
  92.  
  93.             Console.WriteLine(string.Join(" ", readySalads));
  94.             Console.WriteLine(string.Join(" ", remainingItem));
  95.  
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement