Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- *
- * Answer to the following question:
- *
- * selam,
- * diyelim guzellik yarismasinda 20 kisi var. 10 kisi bir sonraki tura cikacak. ben de
- * 5 kisilik tahmin yapiyorum. jurinin sectigi 10 arasinda benim listededen 4 ve üstü
- * (4 ve 5 yani) kisi olmasinin olasiligi nedir?
- *
- */
- public class OlasilikSorusu {
- public static void main(String[] args){
- int[] nominees = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
- int[] jurysChoices = new int[10];
- int[] myGuess = new int[5];
- int myScore = 0; // number of nominees i guessed right
- int myOverallChance = 0; // the number of runs i scores at least 4
- // jury decides only once
- jurysChoices = pickNRandom(nominees, 10);
- // // see how my random guess looks like
- // myGuess = pickNRandom(nominees, 5);
- // printArray(jurysChoices);
- // printArray(myGuess);
- // System.out.println(numberOfIntersection(jurysChoices, myGuess));
- // make 10000 guesses, see success rate
- int numberOfRuns = 10000;
- for(int i=0; i<numberOfRuns; i++){
- myGuess = pickNRandom(nominees, 5);
- myScore = numberOfIntersection(jurysChoices, myGuess);
- if(myScore >= 4 ){
- myOverallChance ++;
- }
- }
- System.out.println("I scored at least 4" + " in " + myOverallChance + "/" + numberOfRuns + " runs.");
- }
- public static int[] pickNRandom(int[] array, int n) {
- List<Integer> list = new ArrayList<Integer>(array.length);
- for (int i : array)
- list.add(i);
- Collections.shuffle(list);
- int[] answer = new int[n];
- for (int i = 0; i < n; i++)
- answer[i] = list.get(i);
- Arrays.sort(answer);
- return answer;
- }
- // some very crude search
- public static int numberOfIntersection(int[] biggerArray, int[] smallerArray){
- int intersectionCount = 0;
- for(int i=0; i<smallerArray.length; i++){
- for(int j:biggerArray){
- if(smallerArray[i] == j){
- intersectionCount++;
- break;
- }
- }
- }
- return intersectionCount;
- }
- public static void printArray(int[] array){
- String s = "";
- for(int i: array){
- s = s + i + " ";
- }
- System.out.println(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement