svetlozar_kirkov

Couples Frequency

Feb 5th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class CouplesFrequency {
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.         Scanner input = new Scanner(System.in);
  12.         String[] inputNums = input.nextLine().split(" ");
  13.         Map<String, Integer> couples = new LinkedHashMap<>();
  14.         for (int i = 0; i < inputNums.length-1; i++){
  15.             String temp = inputNums[i]+" "+inputNums[i+1];
  16.             if (couples.containsKey(temp)){
  17.                 //int oldCount = couples.get(temp);
  18.                 //couples.put(temp, oldCount+1);
  19.                 couples.computeIfPresent(temp, (k, v) -> v + 1);
  20.             }
  21.             else {
  22.                 couples.put(temp, 1);
  23.             }
  24.         }
  25.         for(Map.Entry<String,Integer> entry : couples.entrySet()) {
  26.             String key = entry.getKey();
  27.             Integer value = entry.getValue();
  28.             double length = inputNums.length-1;
  29.             double percentage = (value*100)/length;
  30.             System.out.printf("%s -> %.2f%%\n",key,percentage);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment