Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ БЕЗ ЦИКЪЛ:
- import java.util.Scanner;
- public class Coins {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double input = Double.parseDouble(scanner.nextLine());
- int change = (int) (input * 100), coinCount = 0;
- if (change >= 200) {
- coinCount += change / 200;
- change %= 200;
- }
- if (change >= 100) {
- coinCount += change / 100;
- change %= 100;
- }
- if (change >= 50) {
- coinCount += change / 50;
- change %= 50;
- }
- if (change >= 20) {
- coinCount += change / 20;
- change %= 20;
- }
- if (change >= 10) {
- coinCount += change / 10;
- change %= 10;
- }
- if (change >= 5) {
- coinCount += change / 5;
- change %= 5;
- }
- if (change >= 2) {
- coinCount += change / 2;
- change %= 2;
- }
- if (change == 1) {
- coinCount++;
- change--;
- }
- System.out.println(coinCount);
- }
- }
- РЕШЕНИЕ С WHILE / FOR:
- import java.util.Scanner;
- public class Coins {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double input = Double.parseDouble(scanner.nextLine());
- int change = (int) (input * 100), coinCount = 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--;
- }
- coinCount++;
- }
- System.out.println(coinCount);
- }
- }
- ИЛИ:
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- import java.util.Scanner;
- public class Coins {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double input = Double.parseDouble(scanner.nextLine());
- int change = (int) (input * 100), coinCount = 0;
- while (change > 0) { // ИЛИ: for (int i = 0; change > 0; i++) {
- 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;
- coinCount++;
- }
- }
- System.out.println(coinCount);
- }
- }
- ОПТИМИЗИРАНО РЕШЕНИЕ:
- import java.util.Scanner;
- public class Coins {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double input = Double.parseDouble(scanner.nextLine());
- int change = (int) (input * 100), coinCount = 0;
- for (int coin : new int[]{200, 100, 50, 20, 10, 5, 2, 1}) {
- if (change >= coin) {
- coinCount += change / coin;
- change %= coin;
- }
- }
- System.out.println(coinCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement