Reference Solution for FRQ of 2024 Asia Set(Updated)

4 阅读1分钟

Question 1

  • Part a
   public static int ballThrow() {
        int score = (int) (Math.random() * 5) + 1;
        return score * 10;
   }
  • Part b
   public static double averageThrow(int numThrows, int minScore) {

        double sum = 0.0;
        int count = 0;

        for (int i = 0; i < numThrows; i++) {
            int score = ballThrow();
            if (score > minScore) {
                sum += score;
                count++;
            }
        }

        if (count == 0) {
            return 0.0;
        } else {
            return sum / count;
        }
    }

Question 2

Note: question 2 is ignored since it is about inheritance implementation.

Question 3

  • Part a
   public static ArrayList<Integer> allInversions(int[] numbers) {

        ArrayList<Integer> list = new ArrayList<Integer>();

        for (int i = 0; i < numbers.length - 1; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                if (numbers[i] > numbers[j]) {
                    list.add(numbers[i]);
                    list.add(numbers[j]);
                }
            }
        }

        return list;
    }
  • Part b
   public static int valueWithMostInversions(int[] numbers) {

        ArrayList<Integer> list = allInversions(numbers);

        int valueWithMostInversions = numbers[0];
        int maxCount = countOccur(list, numbers[0]);

        for (int i = 1; i < numbers.length; i++) {
            
            int count = countOccur(list, numbers[i]);
            
            if (count > maxCount) {
                maxCount = count;
                valueWithMostInversions = numbers[i];
            }
        }

        return valueWithMostInversions;
    }

Question 4

  • Part a
   public TreasureMap(int r, int c, ArrayList<Location> locs) {

        map = new Treasure[r][c];

        for (int i = 0; i < locs.size(); i++) {
            int row = locs.get(i).getRow();
            int col = locs.get(i).getCol();
            map[row][col] = new Treasure();
        }
    }
  • Part b
   public int totalGold(Location start, Location end) {

        int sum = 0;

        for (int r = start.getRow(); r <= end.getRow(); r++) {
            for (int c = start.getCol(); c <= end.getCol(); c++) {	
                if (map[r][c] != null) {
                    sum += map[r][c].getGold();
                }
            }
        }

        return sum;
   }