Venciity

Java exam - 04.CouplesFrequency

Jun 3rd, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map.Entry;
  3. import java.util.Scanner;;
  4.  
  5. public class _04_CouplesFrequency {
  6.  
  7.     public static void main(String[] args) {
  8.         @SuppressWarnings("resource")
  9.         Scanner input = new Scanner(System.in);
  10.        
  11.         String sequence = input.nextLine();
  12.         String[] numbers = sequence.split(" ");
  13.        
  14.         LinkedHashMap<String, Double> map = new LinkedHashMap<String , Double>();
  15.        
  16.         for (int i = 0; i < numbers.length - 1; i++) {
  17.             String couple = numbers[i] + " " + numbers[i+1];
  18.            
  19.             if (!map.containsKey(couple)) {
  20.                 map.put(couple, 1.0);
  21.             }
  22.             else {
  23.                 map.put(couple, map.get(couple) + 1.0);
  24.             }
  25.         }
  26.        
  27.         for (Entry<String, Double> entry : map.entrySet()) {
  28.             System.out.printf("%s -> %.2f%%", entry.getKey(),
  29.                     (entry.getValue() / (numbers.length - 1)) * 100);
  30.             System.out.println();
  31.         }
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment