Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
2
Indexable
Never
import java.util.ArrayList;
import java.util.HashMap;

public class andmed {
    public static void main(String[] args) {
        //create map with keys from 1-50 and values of 0
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 1; i < 51; i++) {
            map.put(i,0);
        }
        int loendur=0;
        for (int u = 0; u < 500000 ;u++) {
            for (int i = 0; i < 250; i++) {
                ArrayList<Integer> list = new ArrayList<Integer>();
                //generte 5 distinct random numbers from 1-50
                while (list.size()<5){
                    int random = (int) (Math.random()*50+1);
                    if (!list.contains(random)){
                        list.add(random);
                    }
                }
                //add 1 to the value of each key in map that is in list
                for (int j = 0; j < list.size(); j++) {
                    map.put(list.get(j),map.get(list.get(j))+1);
                }
                //find if any value in map is <=18 and any value in map is >=43
                boolean b1 = false;
                boolean b2 = false;
                for (int j = 0; j < list.size(); j++) {
                    if (map.get(list.get(j))<=18){
                        b1=true;
                    }
                    if (map.get(list.get(j))>=43){
                        b2=true;
                    }
                }
                //if both are true, add 1 to loendur
                if (b1 && b2){
                    loendur++;
                }

            }
            map.replaceAll( (k,v)->v=0 );
        }
        System.out.println(loendur);

    }
}