MartinPaunov

Coins

Jul 4th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         // Read input as double
  9.         double input = double.Parse(Console.ReadLine());
  10.  
  11.         // Here we calculate the input and remove the floating point value
  12.         // we want to use the value as integer so we cast it
  13.         int change = (int)(input * 100);
  14.  
  15.         // We initialize a counter that will increase each time the loop iterates
  16.         int counter = 0;
  17.  
  18.         // While there is something to be taken from the change we iterate and increase the counter
  19.         while (change > 0)
  20.         {
  21.             // Note that coins are represented as integer value which means coin of 2 = 200,
  22.             // coin of 1 = 100, coin of 0.50 = 50 -> and so on
  23.             if (change >= 200)      // We start with the max coin value so we can get minimum sum of total                          
  24.             {                       // coins taken this is simple example of Greedy Algorithm
  25.                 change -= 200;
  26.             }
  27.             else if (change >= 100) // Then we continue with the next coin in descending order
  28.             {                      
  29.                 change -= 100;    
  30.             }
  31.             else if (change >= 50)
  32.             {
  33.                 change -= 50;
  34.             }
  35.             else if (change >= 20)
  36.             {
  37.                 change -= 20;
  38.             }
  39.             else if (change >= 10)
  40.             {
  41.                 change -= 10;
  42.             }
  43.             else if (change >= 5)
  44.             {
  45.                 change -= 5;
  46.             }
  47.             else if (change >= 2)
  48.             {
  49.                 change -= 2;
  50.             }
  51.             else if (change >= 1)
  52.             {
  53.                 change -= 1;
  54.             }
  55.             // Each time the loop iterates we increase total count of coins taken
  56.             counter++;
  57.         }
  58.         // We print the total count of coins taken
  59.         Console.WriteLine(counter);
  60.  
  61.         // P. S. Note, that this can be done with easier calculations
  62.         // figure them out :)
  63.  
  64.         // This solution won't work for input 2.01, yea that's because computers hate floating point values
  65.         // There is a way to make this work for any value :)
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment