Advertisement
borkins

Exam 07-Jan-2018 - 06A2. Coins

Feb 4th, 2018
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. /**
  2.  * Project: Exam_07_January_2018 - created by borkins on 2018-02-05.
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _06a_Coins {
  8.     public static double n;
  9.    
  10.     // Formatting Java Double to C# Double
  11.     public static String getCSharpDouble(double number) {
  12.         StringBuilder sb = new StringBuilder();
  13.         String str = "" + number;
  14.         char sp = new java.text.DecimalFormatSymbols().getDecimalSeparator();
  15.         int len = (str.length() > 15) ? 15 : str.length();
  16.        
  17.         for (int i = 0; i < len; i++) {
  18.             sb.append((str.charAt(i) != sp) ? '#' : sp);
  19.         }
  20.         return new java.text.DecimalFormat("" + sb).format(number);
  21.     }
  22.    
  23.     public static long count(int coinValue) {
  24.         long coins = (long) Double.parseDouble(getCSharpDouble(n / coinValue));
  25.         n = (long) Double.parseDouble(getCSharpDouble(n - coins * coinValue));
  26.         return coins;
  27.     }
  28.    
  29.     public static void main(String[] args) {
  30.         Scanner scan = new Scanner(System.in);
  31.        
  32.         n = Double.parseDouble(scan.nextLine()) * 100.0;
  33.         long totalCoins = 0;
  34.        
  35.         totalCoins += count(200);
  36.         totalCoins += count(100);
  37.         totalCoins += count(50);
  38.         totalCoins += count(20);
  39.         totalCoins += count(10);
  40.         totalCoins += count(5);
  41.         totalCoins += count(2);
  42.         totalCoins += count(1);
  43.        
  44.         System.out.println(totalCoins);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement