teddy-popova

Номинали на монети – GreedyAlgorithm

Nov 29th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class coins {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.            System.out.println("Въведи сумата:");
  8.            int sum = scan.nextInt();
  9.            System.out.print("Брой монети: ");
  10.            System.out.println(count(sum));
  11.  
  12.     }
  13.     public static int count(int sum) {
  14.      int coin = 0;
  15.      int i = 0 ;
  16.      int[] nom = {50, 20, 10, 5, 2, 1};
  17.      while (sum > 0) {
  18.          if (sum/nom[i] > 0){
  19.              sum -= nom[i];
  20.              coin++;
  21.          }else {
  22.              i++;
  23.          }
  24.      }
  25.      return coin;
  26.  }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment