Advertisement
Guest User

MakeASalad

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MakeASalad
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, int> vegetablesCalories = new Dictionary<string, int>()
  12.             {
  13.                 {"tomato",80 },
  14.                 {"carrot",136 },
  15.                 {"lettuce",109 },
  16.                 {"potato",215 }
  17.             };
  18.             string[] inputVegetables =
  19.                 Console.ReadLine()
  20.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  21.                 .Where(x => vegetablesCalories.ContainsKey(x))
  22.                 .ToArray();
  23.             int[] inputSalads =
  24.                 Console.ReadLine()
  25.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  26.                 .Select(int.Parse)
  27.                 .ToArray();
  28.             Queue<string> vegetables = new Queue<string>(inputVegetables);
  29.             Stack<int> salads = new Stack<int>(inputSalads);
  30.             List<int> saladsReady = new List<int>();
  31.             int caloriesRest = 0;
  32.             while (vegetables.Any() &&salads.Any())
  33.             {
  34.                 int saladCalories = salads.Peek();
  35.                 while (saladCalories >= 0 && vegetables.Any())
  36.                 {
  37.                     saladCalories -= vegetablesCalories[vegetables.Dequeue()];
  38.                 }
  39.                 saladCalories -= caloriesRest;
  40.  
  41.                 caloriesRest = Math.Abs(saladCalories);
  42.                 saladsReady.Add(salads.Pop());
  43.             }
  44.             Console.WriteLine(string.Join(' ', saladsReady));
  45.             if (vegetables.Any())
  46.             {
  47.                 Console.WriteLine(string.Join(' ', vegetables));
  48.             }
  49.             if (salads.Any())
  50.             {
  51.                 Console.WriteLine(string.Join(' ', salads));
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement