borkins

Exam 07-Jan-2018 - 06A1. Coins

Feb 4th, 2018
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 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.     // Formatting Java Double to C# Double
  9.     public static String getCSharpDouble(double number) {
  10.         StringBuilder sb = new StringBuilder();
  11.         String str = "" + number;
  12.         char sp = new java.text.DecimalFormatSymbols().getDecimalSeparator();
  13.         int len = (str.length() > 15) ? 15 : str.length();
  14.        
  15.         for (int i = 0; i < len; i++) {
  16.             sb.append((str.charAt(i) != sp) ? '#' : sp);
  17.         }
  18.         return new java.text.DecimalFormat("" + sb).format(number);
  19.     }
  20.    
  21.     public static void main(String[] args) {
  22.         Scanner scan = new Scanner(System.in);
  23.        
  24.         double change = Double.parseDouble(scan.nextLine());
  25.         long totalCoins = 0;
  26.        
  27.         for (int i = 0; i < 3; i++) {
  28.             /* Add coins:
  29.              *  2,  1  leva    (i = 0)
  30.              *  20, 10 cents   (i = 1)
  31.              *  2,  1  cents   (i = 2)
  32.              */
  33.             // The bitwise '& 1' checks for odd digit
  34.             totalCoins += ((long) change / 2) + ((long) change & 1);
  35.            
  36.             change *= 10; // Get the next digit
  37.             // Add coins: 50, 5 cents (i < 2)
  38.             if (change % 10 >= 5 && i < 2) {
  39.                 totalCoins++;
  40.                 change -= 5;
  41.             }
  42.             // Truncate the front digits
  43.             change = Double.parseDouble(getCSharpDouble(((change * 10) % 100) / 10));
  44.         }
  45.        
  46.         System.out.println(totalCoins);
  47.     }
  48. }
Add Comment
Please, Sign In to add comment