Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package javaapplication28;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Map;
  8. import java.util.Random;
  9. import java.util.TreeMap;
  10.  
  11. public class JavaApplication28 {
  12.  
  13.     public static void main(String[] args) {
  14.         int[] tab1 = new int[100];
  15.         int[] tab2 = new int[100];
  16.         Map<Integer, Integer> licznosc;
  17.  
  18.         wypelnijTab(tab1);
  19.         wypelnijTab(tab2);
  20.         licznosc = licznosc(tab1, tab2);
  21.         zapiszLicznosc(licznosc);
  22.  
  23.     }
  24.  
  25.     static Map licznosc(int[] tab1, int[] tab2) {
  26.         Map<Integer, Integer> licznosc = new TreeMap<>();
  27.         for (int liczba : tab1) {
  28.             if (jestPierwsza(liczba)) {
  29.                 if (licznosc.containsKey(liczba)) {
  30.                     int ilosc = licznosc.get(liczba);
  31.                     ilosc++;
  32.                     licznosc.put(liczba, ilosc);
  33.                 } else {
  34.                     licznosc.put(liczba, 1);
  35.                 }
  36.             }
  37.         }
  38.         for (int liczba : tab2) {
  39.             if (jestPierwsza(liczba)) {
  40.                 if (licznosc.containsKey(liczba)) {
  41.                     int ilosc = licznosc.get(liczba);
  42.                     ilosc++;
  43.                     licznosc.put(liczba, ilosc);
  44.                 } else {
  45.                     licznosc.put(liczba, 1);
  46.                 }
  47.             }
  48.         }
  49.         return licznosc;
  50.     }
  51.  
  52.     static <K, V> void zapiszLicznosc(Map<K, V> licznosc) {
  53.         File plik = new File("licznosc.txt");
  54.         BufferedWriter br = null;
  55.         try {
  56.             br = new BufferedWriter(new FileWriter(plik));
  57.             for (Map.Entry<K, V> wiersz : licznosc.entrySet()) {
  58.                 br.write(wiersz.getKey() + ":" + wiersz.getValue());
  59.                 br.newLine();
  60.             }
  61.         } catch (IOException ex) {
  62.         }
  63.         if (br != null) {
  64.             try {
  65.                 br.close();
  66.             } catch (IOException ex) {
  67.             }
  68.         }
  69.     }
  70.  
  71.     static void wypelnijTab(int[] tab) {
  72.         Random rng = new Random();
  73.         for (int i = 0; i < tab.length; i++) {
  74.             tab[i] = rng.nextInt(20) + 1;
  75.         }
  76.  
  77.     }
  78.  
  79.     static boolean jestPierwsza(int n) {
  80.         if (n == 1) {
  81.             return false;
  82.         }
  83.         for (int i = 2; i < n; i++) {
  84.             if (n % i == 0) {
  85.                 return false;
  86.             }
  87.         }
  88.         return true;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement