Advertisement
Petra_Abrasheva_185

Greedy algoritm - moneti

Nov 5th, 2020
1,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.         //point 1
  7.         System.out.println("МОЛЯ ВЪВЕДЕТЕ СЪМАТА :");
  8.         int sum = input.nextInt();
  9.         CountNominees(sum);
  10.  
  11.     }
  12.     //end of main
  13.     public static void CountNominees(int sum) {
  14.         int counter = 0;//брояч за монетите
  15.         int index = 0;//индекс за масива с номинали
  16.         int[] nominees = {50, 20, 10, 5, 2, 1};//масив с номинали
  17.  
  18.         while (sum > 0) {
  19.             if (sum / nominees[index] > 0) {
  20.                 sum = sum - nominees[index];
  21.                 counter++;
  22.                 System.out.println(sum + nominees[index] + "(-" + nominees[index] + " )[БРОЯЧ: " + counter + "]");
  23.             } else
  24.                 index++;//преминаване на следващия номинал от масива с номинали
  25.             System.out.println();
  26.         }
  27.         System.out.println("БРОЯТ НА МОНЕТИТЕ Е:" + counter);
  28.     }
  29. }//end of class
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement