Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Coins
- {
- class Program
- {
- static void Main(string[] args)
- {
- int change = (int)(double.Parse(Console.ReadLine()) * 100);
- int coins = 0;
- while (change > 0) //ИЛИ: for (int i = 0; change > 0; i++)
- {
- if (change >= 200)
- {
- change -= 200;
- }
- else if (change >= 100)
- {
- 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
- {
- change--;
- }
- coins++;
- }
- Console.WriteLine(coins);
- }
- }
- }
- РЕШЕНИЕ БЕЗ ЦИКЪЛ:
- using System;
- namespace Coins
- {
- class Program
- {
- static void Main(string[] args)
- {
- int change = (int)(double.Parse(Console.ReadLine()) * 100);
- int coins = 0;
- if (change >= 200)
- {
- coins += (int)(change / 200);
- change %= 200;
- }
- if (change >= 100)
- {
- coins += (int)(change / 100);
- change %= 100;
- }
- if (change >= 50)
- {
- coins += (int)(change / 50);
- change %= 50;
- }
- if (change >= 20)
- {
- coins += (int)(change / 20);
- change %= 20;
- }
- if (change >= 10)
- {
- coins += (int)(change / 10);
- change %= 10;
- }
- if (change >= 5)
- {
- coins += (int)(change / 5);
- change %= 5;
- }
- if (change >= 2)
- {
- coins += (int)(change / 2);
- change %= 2;
- }
- if (change == 1)
- {
- coins++;
- change--;
- }
- Console.WriteLine(coins);
- }
- }
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- using System;
- namespace Coins
- {
- class Program
- {
- static void Main(string[] args)
- {
- int change = (int)(double.Parse(Console.ReadLine()) * 100);
- int coins = 0;
- while (change != 0)
- {
- change -= change >= 200 ? 200
- : change >= 100 ? 100
- : change >= 50 ? 50
- : change >= 20 ? 20
- : change >= 10 ? 10
- : change >= 5 ? 5
- : change >= 2 ? 2 : 1;
- coins++;
- }
- Console.WriteLine(coins);
- }
- }
- }
- РЕШЕНИЕ С МАСИВ:
- using System;
- namespace Coins
- {
- class Program
- {
- static void Main(string[] args)
- {
- int change = (int)(double.Parse(Console.ReadLine()) * 100);
- int[] coinValues = new[] { 200, 100, 50, 20, 10, 5, 2, 1 };
- int coins = 0;
- for (int i = 0; i < coinValues.Length; i++)
- {
- if (change >= coinValues[i])
- {
- coins += (int)(change / coinValues[i]);
- change %= coinValues[i];
- }
- }
- Console.WriteLine(coins);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement