Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- public class _4_CouplesFrequency {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- String inputLine = scan.nextLine().trim();
- String[] inputNums = inputLine.split(" ");
- LinkedHashMap<String, Integer> frequenciesMap = new LinkedHashMap<>();
- String couple;
- Integer times;
- for (int i = 0; i + 1 < inputNums.length; i++) {
- couple = inputNums[i] + " " + inputNums[i + 1];
- times = frequenciesMap.get(couple);
- if (times == null) {
- times = 0;
- }
- frequenciesMap.put(couple, times + 1);
- }
- float totalOccurrences = inputNums.length - 1;
- printTheFrequenciesOfAllCouples(frequenciesMap, totalOccurrences);
- }
- private static void printTheFrequenciesOfAllCouples(
- LinkedHashMap<String, Integer> frequenciesMap, float allOccurrences) {
- float percentage;
- for (String couple : frequenciesMap.keySet()) {
- System.out.print(couple + " -> ");
- percentage = (frequenciesMap.get(couple) * 100) / allOccurrences;
- System.out.printf("%.2f", percentage);
- System.out.println("%");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement