Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- // Read input as double
- double input = double.Parse(Console.ReadLine());
- // Here we calculate the input and remove the floating point value
- // we want to use the value as integer so we cast it
- int change = (int)(input * 100);
- // We initialize a counter that will increase each time the loop iterates
- int counter = 0;
- // While there is something to be taken from the change we iterate and increase the counter
- while (change > 0)
- {
- // Note that coins are represented as integer value which means coin of 2 = 200,
- // coin of 1 = 100, coin of 0.50 = 50 -> and so on
- if (change >= 200) // We start with the max coin value so we can get minimum sum of total
- { // coins taken this is simple example of Greedy Algorithm
- change -= 200;
- }
- else if (change >= 100) // Then we continue with the next coin in descending order
- {
- change -= 100;
- }
- else if (change >= 50)
- {
- change -= 50;
- }
- else if (change >= 20)
- {
- change -= 20;
- }
- else if (change >= 10)
- {
- change -= 10;
- }
- else if (change >= 5)
- {
- change -= 5;
- }
- else if (change >= 2)
- {
- change -= 2;
- }
- else if (change >= 1)
- {
- change -= 1;
- }
- // Each time the loop iterates we increase total count of coins taken
- counter++;
- }
- // We print the total count of coins taken
- Console.WriteLine(counter);
- // P. S. Note, that this can be done with easier calculations
- // figure them out :)
- // This solution won't work for input 2.01, yea that's because computers hate floating point values
- // There is a way to make this work for any value :)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment