Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class CouplesFrequency {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String[] inputNums = input.nextLine().split(" ");
- Map<String, Integer> couples = new LinkedHashMap<>();
- for (int i = 0; i < inputNums.length-1; i++){
- String temp = inputNums[i]+" "+inputNums[i+1];
- if (couples.containsKey(temp)){
- //int oldCount = couples.get(temp);
- //couples.put(temp, oldCount+1);
- couples.computeIfPresent(temp, (k, v) -> v + 1);
- }
- else {
- couples.put(temp, 1);
- }
- }
- for(Map.Entry<String,Integer> entry : couples.entrySet()) {
- String key = entry.getKey();
- Integer value = entry.getValue();
- double length = inputNums.length-1;
- double percentage = (value*100)/length;
- System.out.printf("%s -> %.2f%%\n",key,percentage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment