Advertisement
Aborigenius

ShoppingSpree

Aug 15th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ShoppingSpree
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             decimal budget = decimal.Parse(Console.ReadLine());
  14.             Dictionary<string, decimal> products = new Dictionary<string, decimal>();
  15.             string input = Console.ReadLine();
  16.  
  17.             while (input != "end")
  18.             {
  19.                 string[] inputTokens = input.Split(' ');
  20.  
  21.                 string prodKey = inputTokens[0];
  22.                 decimal prodVal = decimal.Parse(inputTokens[1]);
  23.  
  24.                 if (!products.ContainsKey(prodKey) || prodVal < products[prodKey])
  25.                 {
  26.                     products[prodKey] = prodVal;
  27.                 }
  28.  
  29.                 input = Console.ReadLine();
  30.             }
  31.  
  32.             decimal productsSum = products.Values.Sum();
  33.  
  34.      
  35.          
  36.  
  37.             if (budget >= productsSum)
  38.             {
  39.                 foreach (var item in products.OrderByDescending(item => item.Value)
  40.             .ThenBy(x => x.Key.Length))
  41.                 {
  42.                     Console.WriteLine($"{item.Key:f2} costs {item.Value:f2}");
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 Console.WriteLine("Need more money... Just buy banichka");
  48.             }
  49.  
  50.  
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement