Given a list of the scores of different students, items
, where items[i] = [IDi, scorei]
represents one score from a student with IDi
, calculate each student's top five average.
Return the answer as an array of pairs result
, where result[j] = [IDj, topFiveAveragej]
represents the student with IDj
and their top five average. Sort result
by IDj
in increasing order.
A student's top five average is calculated by taking the sum of their top five scores and dividing it by 5
using integer division.
Example 1:
Input: items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]] Output: [[1,87],[2,88]] Explanation: The student with ID = 1 got scores 91, 92, 60, 65, 87, and 100. Their top five average is (100 + 92 + 91 + 87 + 65) / 5 = 87. The student with ID = 2 got scores 93, 97, 77, 100, and 76. Their top five average is (100 + 97 + 93 + 77 + 76) / 5 = 88.6, but with integer division their average converts to 88.
Example 2:
Input: items = [[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100]] Output: [[1,100],[7,100]]
Constraints:
1 <= items.length <= 1000
items[i].length == 2
1 <= IDi <= 1000
0 <= scorei <= 100
IDi
, there will be at least five scores.program main
implicit none
integer :: i, j, n
integer, allocatable :: ids(:), scores(:)
real(kind=8), allocatable :: result(:,:)
! read input
read(*,*) n
allocate(ids(n))
allocate(scores(n))
do i = 1, n
read(*,*) ids(i), scores(i)
end do
! solve problem
allocate(result(n,2))
do i = 1, n
result(i,1) = ids(i)
result(i,2) = topFiveAverage(scores(i))
end do
! sort result by id in increasing order
call sort(result, n, 2, 1)
! print output
do i = 1, n
write(*,fmt='(I4, F6.2)') result(i,1), result(i,2)
end do
contains
function topFiveAverage(score) result(average)
implicit none
integer, intent(in) :: score
integer :: topScores(5)
integer :: i, sum
! find top five scores
topScores = score
do i = 2, size(topScores)
if (topScores(i-1) < score) then
topScores(i) = topScores(i-1)
topScores(i-1) = score
end if
end do
! calculate top five average
sum = 0
do i = 1, size(topScores)
sum = sum + topScores(i)
end do
average = sum / 5
end function topFiveAverage
end program main
temp.f95:33:50: 33 | function topFiveAverage(score) result(average) | 1 Error: Function result โaverageโ at (1) has no IMPLICIT type temp.f95:20:22: 20 | result(i,2) = topFiveAverage(scores(i)) | 1 Error: Cannot convert UNKNOWN to REAL(8) at (1)
module top_five_average
implicit none
contains
subroutine solve(items, result)
integer, intent(in) :: items(:, :)
integer, intent(out) :: result(:, :)
! Declare variables
integer :: i, j, k, n, m, id, score, sum, avg
integer, allocatable :: scores(:)
! Initialize variables
n = size(items, 1)
m = size(items, 2)
allocate(scores(m))
! Sort the scores in increasing order
do i = 1, n
do j = 1, m
scores(j) = items(i, j)
end do
call sort(scores)
! Calculate the top five average
sum = 0
k = 1
do while (k <= 5 .and. k <= m)
sum = sum + scores(k)
k = k + 1
end do
avg = sum / 5
! Store the result
id = items(i, 1)
result(i, :) = [id, avg]
end do
! Sort the result by ID in increasing order
call sort(result, 1)
end subroutine solve
subroutine sort(array, dim)
integer, intent(inout) :: array(:, :)
integer, intent(in) :: dim
! Declare variables
integer :: i, j, temp
! Bubble sort algorithm
do i = 1, size(array, 1) - 1
do j = 1, size(array, 1) - i
if (array(j, dim) > array(j + 1, dim)) then
temp = array(j, dim)
array(j, dim) = array(j + 1, dim)
array(j + 1, dim) = temp
end if
end do
end do
end subroutine sort
end module top_five_average
program test
use top_five_average
implicit none
! Test case 1
integer, parameter :: items1(10, 2) = reshape([1, 91, 1, 92, 2, 93, 2, 97, 1, 60, 1, 65, 1, 87, 1, 100, 2, 2, 77, 2, 100, 2, 76], [10, 2])
integer, allocatable :: result1(:, :)
call solve(items1, result1)
write (*, *) "Test case 1:"
write (*, *) "Expected: [[1,87],[2,88]]"
write (*, *) "Actual: ", result1
write (*, *) ""
! Test case 2
integer, parameter :: items2(10, 2) = reshape([1, 100, 7, 100, 1, 100, 7, 100, 1, 100, 7, 100], [10, 2])
integer, allocatable :: result2(:, :)
call solve(items2, result2)
write (*, *) "Test case 2:"
write (*, *) "Expected: [[1,100],[7,100]]"
write (*, *) "Actual: ", result2
write (*, *) ""
end program test
temp.f95:25:18: 25 | call sort(scores) | 1 Error: Rank mismatch in argument โarrayโ at (1) (rank-2 and rank-1) temp.f95:69:9: 69 | use top_five_average | 1 Fatal Error: Cannot open module file โtop_five_average.modโ for reading at (1): No such file or directory compilation terminated.
def alice_win(n):
return n % 2 == 0
The game is a simple parity-based game, where Alice and Bob alternately change the number on the board. Since Alice goes first, if the number on the board is even initially, Alice has the opportunity to make a move and maintain the even number status of the board. If Alice and Bob are playing optimally, they will always choose a number that is a divisor of the current number by 2, to guarantee they leave an even number on the board. In this way, the parity of the number on the board never changes.
So, when the number becomes 1, the player whose turn it is will lose, as they cannot choose a divisor of the current number that is less than the current number. Since the number remains even throughout the game, if it starts as an even number, Alice will always win; if it starts as an odd number, Bob will always win. The algorithm simply checks if the given number is even and returns true if Alice wins, and false otherwise. The code implementations for each language perform the modulo operation %
to check if the given number is even.
bool aliceWin(int n) {
return n % 2 == 0;
}
The game is a simple parity-based game, where Alice and Bob alternately change the number on the board. Since Alice goes first, if the number on the board is even initially, Alice has the opportunity to make a move and maintain the even number status of the board. If Alice and Bob are playing optimally, they will always choose a number that is a divisor of the current number by 2, to guarantee they leave an even number on the board. In this way, the parity of the number on the board never changes.
So, when the number becomes 1, the player whose turn it is will lose, as they cannot choose a divisor of the current number that is less than the current number. Since the number remains even throughout the game, if it starts as an even number, Alice will always win; if it starts as an odd number, Bob will always win. The algorithm simply checks if the given number is even and returns true if Alice wins, and false otherwise. The code implementations for each language perform the modulo operation %
to check if the given number is even.