aleahim

CardsFrequencies

May 25th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class CardsFrequencies {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.        
  10.         String[] cards_arr = input.nextLine().split("[ ♥♣♦♠]+");
  11.        
  12.         Map<String, Integer> cardsMap = new LinkedHashMap<String, Integer>();
  13.        
  14.         for (String card : cards_arr) {
  15.             Integer count = cardsMap.get(card);
  16.             if (count == null) {
  17.                 count = 0;
  18.             }
  19.             cardsMap.put(card, count + 1);
  20.         }
  21.        
  22.         for (Map.Entry<String, Integer> entry : cardsMap.entrySet()) {
  23.            
  24.             double precentage = (double) entry.getValue() * 100 / cards_arr.length;
  25.             System.out.printf("%s -> %.2f%%\n", entry.getKey(), precentage);
  26.            
  27.         }
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment