Advertisement
Guest User

Couples Frequency

a guest
Jun 1st, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map.Entry;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class CouplesFrequency {
  7.  
  8.     public static void main(String[] args) {
  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.             } else {
  22.                 map.put(couple, map.get(couple) + 1.0);
  23.             }
  24.         }
  25.        
  26.         for (Entry<String, Double> entry : map.entrySet()) {
  27.             System.out.printf("%s -> %.2f%%", entry.getKey(),
  28.                     (entry.getValue() / (numbers.length - 1)) * 100);
  29.             System.out.println();
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement